Tips about & | expressions in javascript _ javascript skills

Source: Internet
Author: User
I will introduce and parse 12 simple but powerful JavaScript skills. all JavaScript programmers can use these skills immediately. You do not need to become a JavaScript master to understand these skills. next we will start the first article in this series to introduce powerful & amp; and | expressions. If you are a newbie, after reading the details of all these skills and each skill, if you use them after work, you will write more concise and efficient JavaScript programs.

Indeed, JavaScript experts have used these techniques to write many powerful and efficient JavaScript programs, but you can.

Powerful & | expression
You may have seen them in the JavaScript library and JavaScript framework. Let's start with several basic examples:

Example 1. | (OR)
Set the default value.

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 ";
}

Resolution:

First, read the following prompt box to review how JavaScript judges boolean values.
| The operator first checks whether the expression is true or false from the left. If the expression is true, the value returned by the Left expression is immediately returned. If the expression on the left is false, the operator continues to judge the expression on the right, return the value of the expression on the right.
If theTitle is false, the value of the right expression is returned. In other words, if the theTitle variable is true, the value of theTitle is returned.
! Tip:
JavaScript determines false values: null, false, 0, undefined, NaN, and "" (empty string ).
Remember that the NaN class value like Infinity (infinitely large) is determined to be true rather than false. However, NaN is determined to be false.
All other values except the preceding values are regarded as true.

Example 2. & (and)

Do not do this:

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

Use this instead:

The Code is as follows:


Function isAdult (age ){
Return age & age> 17;
}

Resolution:

& Operator judge the expression from the left. If the expression on the left is determined to be false, false is returned immediately regardless of whether the expression on the right is true.
If the expression on the left is true, continue to judge the expression on the right and return the result of the expression on the right.
This is getting more and more interesting.

Example 3.

Do not do this:

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

Use this instead:

The Code is as follows:


UserName & logIn (userName) | signUp ();

Resolution:

If userName is true, call the logIn function and pass the userName variable.
If userName is false, call the logIn function without passing any variables.

Example 4.
Do not do this:

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;

Resolution:

If userName is true, call user. loggedIn and check whether user. loggedIn is true. If true is returned, the return value of the third expression is returned.
If userName is null, null is returned.

The above is the first javascript tips I will share with you in this article. I hope you will like it.

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.