Application of JavaScript logical operator "or"

Source: Internet
Author: User
Tags logical operators

In JavaScript, logical operators are primarily used for Boolean logic operations that return a Boolean value based on the result of an expression's operation. Because parameters do not necessarily have to be true or false (they can contain true or false meanings), the returned result may not be a Boolean value.

To understand this "or" (| | ) operator, let's look at a relatively simple example. In the following example, the OR operator can be used to provide a default value for a newly defined variable:

<!-- lang: js -->    var bar = false,    foobar = 5,    foo = bar || foobar; // foo = 5

In the above example, only when bar contains false, does Foobar pass its own value to Foo. It should be noted here that the types including 0,false,undefined,null,nan and the empty string "" are implied false.

If you have written a jquery plugin, or have used the $.extend () method, you may be familiar with this usage. You will find that this method is often used to contract a default value to an Options object, or to give an exact value.

<!-- lang: js -->function( options ) {    options = $.extend({}, defaults, options || {});}

Some developers prefer to deliberately use or operator to convert the value of implication false uniformly to null. The advantage of doing this is to ensure that the values are empty.

<!-- lang: js -->var foo = bar || null;

Normally, we don't specifically need a null value, but when you do, you don't have to worry about whether the value is the value you want, or whether it must be a null value.

You will also find that some developers choose to use this method to initialize the namespace, unlike the traditional use of namespace = Namespace | | {} to initialize the namespace first and then extend the namespace, we can also directly one step:

<!-- lang: js -->var namespace = namespace || {    utils:{},    core:{}};

Here we briefly introduce the principle. Because JavaScript variables are not strictly typed, a variable initialized with a Boolean operator can also be assigned a numeric value:

<!-- lang: js -->console.log(null || NaN || undefined || false || 0 || 10);  // outputs: 10

What's happening here is not so magical, it's just an expression that steals a bit of laziness. The parser first confirms that the first statement of the expression, NULL, determines that this is a false value of implication, and continues to parse down until it encounters a value that does not contain false values (or the last value if it has not encountered a value that does not imply false values). This is a frequently used parsing technique in a variety of dynamic languages, including JavaScript. But this method does not work with static languages, and if you do this in a static language the system throws a type error directly.

So what is the use of this? As we saw before, if a variable is likely to be empty, the OR operator can be used to provide a default value in the function:

function foo( a, b ){    a = a || 5;    b = b || 6;    console.log( ‘Values:‘ + a + ‘,‘ +b );}

Of course, you can also use the following methods to solve this problem:

if(a && a === 5){    // do something}else{    // do something else}

Why do you write it like this? We can analyze this problem from two aspects. First is readability, some developers think that the If/else statement is more readable, then in the mind to interpret this piece of code does not need to spend more time to understand. I think we can achieve the same result with the If/else statement if we just consider it from the point of preference. The second is performance issues, and developers generally think that if statements are more than | | Operator performance is better, but testing reveals that there is no very big difference between their expressiveness.

Application of JavaScript logical operator "or"

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.