JavaScript logical expression ' && ' and ' | | '

Source: Internet
Author: User

When learning the logical expressions, I learned some points that I didn't understand before and recorded them.

operator "&&"

The "&&" operator can be understood from three different levels:

First layer: When the operands are all Booleans, "&&" performs a Boolean and (and) operation on two values, which returns true only if the first operand and the second operand are true.

The second layer: && can perform Boolean and (and) operations on Truth and value (False,null,undified,0,-0,nan and "").

The third layer: the operator first calculates the value of the left operand, and if the result is a false value, then "&&" simply returns the value of the left operand, and if the left operand of the expression is true, the "&&" operator returns the value of the right operand and evaluates it as the entire expression.

Such as: 

var person={name:"wxt"}; var nperson=null;p erson&&person.name      // return Person.name nperson&&nperson.name    // return Nperson

  operator ' | | '

Same "| |" The value of the first operand is computed first, if the result is true, then the value of the second operand is returned, and the result of the expression evaluates.

  

JavaScript logical expression ' && ' and ' | | '

Related Article

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.