Does JScript Provide a Ternary Operator?

孤街醉人 提交于 2019-12-23 16:08:00

问题


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


回答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.




回答2:


yes it does.

test ? expression1 : expression2



回答3:


Example:

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



回答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.");


来源:https://stackoverflow.com/questions/5260542/does-jscript-provide-a-ternary-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!