Talk about logic in JS and (&&) as well as logic or (| | )

Source: Internet
Author: User

A few days ago saw a function, baffled its solution, woke up this morning to read this JS book, just about the use of the operator, to share the next JS in the &&,| |, and we use the other programming language is a little different.

Directly on the code of that function:

function Whatdoesitdo (mood) {        return mood && "I like this" | | "I don ' t like this";}

When we assign true to mood, it displays "I like this" (A to replace) the assignment false to show "I dont like this" (b to replace), or we assign a string to mood, which also shows a, which is supposed to return true or FALSE, I don't understand, I read the book.

Logic and (&&) operations can be applied to any type of operation, not just Boolean values, and the,&& operator does not necessarily return a Boolean value if there is an operand that is not a Boolean: follow these rules:

Take A&&b as an example:

1 if the first operand is an object, the second operand is returned, and if A is the object returns B,

2, if the second operand is an object, the object is returned only if the first operand evaluates to True

&& operation is a short-circuit operation, as long as the first operand can determine the result is no longer the operation of the second operand, so the first function because it is a false, because it is possible to determine the result is false, no longer operate DD, even if it is undefined, The second function, because the first operand is true does not determine the result, so will continue to operate DD, found that DD undefined, this will be an error, the third function, the first operand is true is also an object, so follow the first one, return "DD"

3, if two are objects that return a second

4, if one is null, returns null

"DD" &&null returns null;false&&null; returns false because the first one has determined the result, so the next operand is no longer executed

5, if one is Nan, the Nan is returned

6, if there is an operand of undefined, return undefined

Logic or (| | ) is similar to the operation of logic, as long as there is one that is not a Boolean value, | | It does not necessarily return a Boolean value, following the rules:

1, if the first is the object to return to the first, we can easily understand the beginning of the function, if mood is an object, the first && operation return "I like this", and then do | | Operation, because (mood&& "Iike This") is an object, so again | |, it returns mood&& "Iike this", so the result is "I like this".

2, if the result of the first evaluation is false, then the second operand is returned

I'm not saying anything.

3, if two operands are objects, return the first

4, if all is null,nan,undefined, then return null,nan,undefined;

For a| | b, if A is true, B is not manipulated, and if a is false the operation will continue B

For null operations,

var c=a| | B

If a is null, then B is returned, if a is not NULL, a direct return of a, a, or a fallback function;

Welcome to my discussion of JS problem I am Rhino

Talk about logic in JS and (&&) as well as logic 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.