Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 views
in Technique[技术] by (71.8m points)

javascript - 为什么(0 <5 <3)返回true?(Why does (0 < 5 < 3) return true?)

I was playing around in jsfiddle.net and I'm curious as to why this returns true?(我在jsfiddle.net上玩,我很好奇为什么这会返回true?)

if(0 < 5 < 3) {
    alert("True");
}

So does this:(这样做:)

if(0 < 5 < 2) {
    alert("True");
}

But this doesn't:(但这不是:)

if(0 < 5 < 1) {
    alert("True");
}

Is this quirk ever useful?(这个怪癖是否有用?)

  ask by punkrockbuddyholly translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Order of operations causes (0 < 5 < 3) to be interpreted in javascript as ((0 < 5) < 3) which produces (true < 3) and true is counted as 1, causing it to return true.(操作顺序导致(0 < 5 < 3)在javascript中被解释为((0 < 5) < 3)其产生(true < 3)并且true被计为1,从而使其返回true。)

This is also why (0 < 5 < 1) returns false, (0 < 5) returns true, which is interpreted as 1 , resulting in (1 < 1) .(这也是为什么(0 < 5 < 1)返回false, (0 < 5)返回true,这被解释为1 ,导致(1 < 1) 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...