Boolean operators in js

Source: Internet
Author: User
When we talked about boolean operators & amp; and |, I said that their results are boolean values. This is a bit too simple. If you use them to calculate boolean data types, they do return boolean values. However, they can also be used to calculate other types of data. As we discussed previously about boolean operators & |, I said that their results are boolean values. This is a bit too simple. If you use them to calculate boolean data types, they do return boolean values. However, they can also be used to calculate other types of data. In this case, the returned data will be one of the parameters. Or the operator "|" is actually doing this: it first checks the parameter on the left of it. If it is converted to a Boolean value, it is true, the parameter on the left is returned. Otherwise, the parameter on the right is returned. Think about this when both sides of the operator are boolean values. Why does it run like this? The running result is actually very practical. Let's look at this example:
var input = prompt("What is your name?", "Kilgore Trout");  alert("Well hello " + (input || "dear"));  

 

If you press "cancel" or directly close the prompt dialog box, the input value is null or "". In both cases, the values converted to the boolean type are false. In this case, the input | "dear" expression means that the input value is obtained when input has a value. Otherwise, the "dear" is obtained ". This is a simple method to provide a default value. The operating principle of the operator "&" is similar to that of "|. When the parameter on the left is converted to a Boolean Value of "false", it returns the value; otherwise, the value on the right is returned. The two operators also have a feature that they calculate the value of the expression on the right only when necessary. In the expression "true | X", no matter what X is, the result is true, so X will not be calculated. If X has other effects, then this effect will not be produced. The same applies to "false & X.
false || alert("I'm happening!");  true || alert("Not me.");  

 

 

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.