JavaScript in about && | | Small Tips for expression sharing

Source: Internet
Author: User

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

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

Strong && | | 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
Set the default value, usually with the

The code is as follows:
1 function Documenttitle (thetitle) {2     if (! thetitle) {3            thetitle = "Untitled Document"; 4     }5 }                    

Use this instead:

The code is as follows:
1 function Documenttitle (thetitle) {2     thetitle = Thetitle | | "Untitled Document"; 3 }

Analytical:

First, read the following "Tips" box to review how JavaScript determines Boolean values
|| The operator first from the left to determine the true and false expression, if true, immediately return the value returned from the left expression; If the left expression is judged false, continue to judge the expression on the right and return the value of the right expression
If Thetitle is judged to be false, it returns the value of the right-hand expression. 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 this 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:

1234567 function Isadult (age) {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 returns false if the left expression is judged false, regardless of whether the expression on the right is true.
If the expression on the left is true, continue judging the right expression and return to the right expression result
It's getting more and more interesting.

Example 3.

Do not do this:

12345 if (userName) {logIn (userName);} else {signUp ();}

Use this instead:

Copy the 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.
Do not do this:

1234567 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 the User.loggedin is true; Returns the return value of the third expression if True is returned
If username is empty, returns null

JavaScript in about && | | Small Tips for expression sharing

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.