user1091856 user1091856 - 1 year ago 129
Javascript Question

Using the ternary operator with string concatenation

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

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

How come it alerts

Answer Source

Try wrapping your parens around the operation

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


what this is doing:

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download