The "JS" logical operator is not! With && or | |

Source: Internet
Author: User

The logical operators in JS are not different from other languages when dealing with Boolean judgments, but when dealing with objects, it is necessary to comb the memory.

Logical NON (!)

Returns False if an operand is an object;

Returns False if an operand is an empty string;

Returns False if an operand is a non-empty string;

Returns true if an operand is a numeric value of 0;

Returns False if an operand is an arbitrary non-0 character;

Returns true if an operand is null;

Returns true if an operand is Nan;

  Returns True if an operand is undefined;

PS: In fact, it is easy to see the value of its logical judgment.

  Logic and (&&)

(1) For Boolean values, logic and is very simple, as long as there is a false, return false;

(2) For cases where the Boolean value is not true:

Returns a second number if the first operand is an object

If the second operand is an object, the object is returned only if the value of the first operand evaluates to true;

If the first two operands are objects, the second operand is returned

Returns null if one of the operands is null

If one of the operands is Nan, the first Nan is returned

Returns undefined if the first operand is undefined

  

PS: In fact, think about it, the logical operator operators follow the order from left to right, the logic and Operators (&&) first to determine the first number, if the first number of logical judgment is true, you also need to determine the second number, the result of the output of the second operand; If the first number is false, then the second number is not considered, directly output the logical judgment result of the first number, which is the same as other language principles.

  Logic or (| |)

(1) For Boolean values, logical or very simple, as long as there is a true, return true;

(2) For cases where the Boolean value is not true:

If the first operand is an object, the first operand is returned

Returns the second operand if the evaluation result of the first operand is false

If two operands are objects, the first operand is returned

Returns NULL if two operands are null

If two operands are Nan, a nan is returned

If two operands are undefined, the undefined is returned

PS: Principle with logic (&&), logic or (| |) The judgment is that if the logic of the first operand evaluates to true, the first operand is output, the second operand is not considered, and the logic of the second operand is determined if the logic of the first operand is false.

The "JS" logical operator is not! With && 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.