Skip to content Skip to sidebar Skip to footer

Does Jscript Provide A Ternary Operator?

Do we have a ternary operator in Jscript (as opposed to JavaScript)? If so, what is the syntax?

Solution 1:

It's

expression?expression:expression

just like C. It's a little looser, actually, because JavaScript is not strongly-typed. Thus the two possible "forks" of the operator can result in different types of values.

Thus:

alert(document.all ? "Hello from IE!" : "Hello from a non-IE browser!");

Most of the time, the differences between Microsoft's ECMAScript and those found in other browsers (or other server-side environments) aren't really that great, and for ordinary non-DOM code it's pretty rare to have to deal with such things.

Solution 2:

yes it does.

test ? expression1 : expression2

Solution 3:

Example:

var result = 5 > 10 ? '5 is greater than 10' : '5 is not greater than 10';

Solution 4:

You can always use google to find language syntax, too.

The first result I got was, http://msdn.microsoft.com/en-us/library/be21c7hw%28v=vs.85%29.aspx. It has examples like

var greeting = "Good" + ((now.getHours() > 17) ? " evening." : " day.");

Post a Comment for "Does Jscript Provide A Ternary Operator?"