最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript triple equals and three-variable comparison - Stack Overflow

programmeradmin0浏览0评论

Can somebody explain this?

1 == 1        //true, as expected
1 === 1       //true, as expected
1 == 1 == 1   //true, as expected
1 == 1 == 2   //false, as expected
1 === 1 === 2 //false, as expected
1 === 1 === 1 //false? <--

Also is there a name for boolean logic that pares more than two numbers in this way (I called it "three-variable parison" but I think that'd be wrong...)

Can somebody explain this?

1 == 1        //true, as expected
1 === 1       //true, as expected
1 == 1 == 1   //true, as expected
1 == 1 == 2   //false, as expected
1 === 1 === 2 //false, as expected
1 === 1 === 1 //false? <--

Also is there a name for boolean logic that pares more than two numbers in this way (I called it "three-variable parison" but I think that'd be wrong...)

Share Improve this question edited Mar 11, 2013 at 5:55 asked Mar 11, 2013 at 5:45 user1318194user1318194 0
Add a ment  | 

2 Answers 2

Reset to default 7

This expression:

1 === 1 === 1

Is evaluated as:

(1 === 1) === 1

After evaluating the expression inside parentheses:

true === 1

And that expression is logically false. The below expression returns true as expected though:

1 === 1 === true

Equality is a left-to-right precedence operation.

So:

1 == 1 == 1
true == 1
true

And:

1 === 1 === 1
true === 1
false // because triple-equals checks type as well
发布评论

评论列表(0)

  1. 暂无评论