JavaScript about && and | | Expression tips for sharing _javascript skills

Source: Internet
Author: User

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

Copy Code code as follows:

function Documenttitle (thetitle) {
if (!thetitle) {
Thetitle = "Untitled Document";
}
}

Use this instead:

Copy Code code 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:

function Isadult {
 if (age && age >) {return
  true;
 } else {return
  false;}
}

Use this instead:

Copy Code code 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:

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:

var UserID;

if (userName && username.loggedin) {
 UserID = username.id;
} else {
 UserID = null;
}

Use this instead:

Copy Code code as follows:

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

Analytical:

If username is true, call User.loggedin and check to see if User.loggedin is true; Returns the return value of the third expression if true
If username is empty, returns null

The above is the article to share the first JavaScript tips, I hope you can enjoy.

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.