JavaScript about && and | | Tips for sharing expressions

Source: Internet
Author: User
Tags continue

If you're still a beginner, and you read all of these tips and each of these tips is to use them later in your work, you'll write more concise and efficient JavaScript programs.

Indeed, JavaScript gurus have used these techniques to write a number of powerful, efficient JavaScript programs. But you can do that.

Powerful && and | | An expression
You may have seen them in JavaScript libraries and JavaScript frameworks, so let's start with a few basic examples:

Example 1. || Or
Sets the default value, usually using the

The

code is as follows:


function Documenttitle (thetitle) {


if (!thetitle) {


thetitle = "Untitled Document";


}


}

Use this instead:

The

code is as follows:


function Documenttitle (thetitle) {


thetitle = Thetitle | | "Untitled Document";


}

Analytical:

First, read the "Tips" box below to review how JavaScript determines Boolean values.
|| Operator first from the left to judge the true and false expression, if true, immediately return the value of the left expression returned; If the left expression is judged to be false, continue to judge the right expression and return the value of the right expression
If Thetitle is judged to be false, the value of the right expression is returned. In other words, if the thetitle variable is judged to be true, the value of Thetitle is returned.
! Tips:
JavaScript evaluates to false values: null, FALSE, 0, undefined, NaN, and "" (empty string).
Remember that the value of the NaN class like Infinity (Infinity) is judged to be true and not false. However, Nan is judged to be false.
In addition to these, all other values are judged to be true.

Example 2. && (and)

Don't do this:

1 2 3 4 5 6 7 function Isadult {if (age && age >) {return true;} else {return false;}}

Use this instead:

The

code is as follows:


function Isadult (age) {


return age && age > 17;


}

Analytical:

The && operator evaluates the expression from the left, and if the left expression is judged false, it returns false immediately, regardless of whether the right expression is true.
If the expression on the left is true, continue to judge the right expression and return to the right expression result
It's getting more and more interesting.

Example 3.

Don't do this:

1 2 3 4 5 if (userName) {logIn (userName);} else {signup ();}

Use this instead:

copy code code as follows:


userName && logIn (userName) | | Signup ();

Analytical:

If username is true, call the login function and pass the username variable
If username is false, calling the login function does not pass any variables

Example 4.
Don't do this:

1 2 3 4 5 6 7 var UserID; if (userName && username.loggedin) {UserID = Username.id;} else {userID = null;}

Use this instead:

The

code is as follows:


var UserID = userName && username.loggedin && username.id;

Analytical:

If username is true, call User.loggedin and check if User.loggedin is true, and return the return value of the third expression if it returns
if username is null, return null

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.