An explanation of ternary operator usage

Source: Internet
Author: User

An explanation of the ternary operator usage:

This operator is a very common element symbol and can save a lot of code if used properly. The ternary operator can also be called as a conditional operator, which can be said to be a simplified form of the If...else statement, the following is a simple example of how to implement the sub-operator, first look at the syntax structure of the operator:

Expression one? Expression two: expression three

A ternary operator is called ternary because it has three operands. When the return value of expression one is true, the value of expression two is returned, otherwise the value of expression three is returned. Let's look at a code example:

<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"/><Metaname= "Author"content= "http://www.softwhy.com/" /><title>Ternary operator usage details-ant tribe</title><styletype= "Text/css">Div{width:200px;Height:50px;margin:0px Auto;Background-color:Green;text-align:Center;Line-height:50px;Color:Red;}</style><Scripttype= "Text/javascript">window.onload=function(){  vara=3, b=4  varm; M=a>b?"a greater than B":"A is less than B"; document.getElementById ("Num"). InnerHTML=m;}</Script></Head><Body><DivID= "num"></Div></Body></HTML>

In the above code, the function of the ternary operator is to determine whether a is greater than B, if greater than the return "a greater than B" otherwise return "a less than B".

The original address is: http://www.51texiao.cn/javascriptjiaocheng/2015/0428/306.html

An explanation of ternary operator usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.