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

Categories

alert("test: "+(1==2)?'hello':'world');

This should show me 'world' on the screen since 1 is not equal to 2.

How come it alerts 'hello'?

See Question&Answers more detail:os

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

1 Answer

Try wrapping your parens around the operation

alert("test: "+ (1 == 2 ? 'hello' : 'world'));

demo: http://jsfiddle.net/hunter/K3PKx/


what this is doing:

alert("test: "+(1==2)?'hello':'world');

is evaluating "test: " + (1==2) as true which outputs 'hello'


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...