JavaScript Novice Learning Notes 4--I can't remember a few pits: short-circuit logic, passing by value, declaring ahead

Source: Internet
Author: User

1. Short Circuit logic

In a logical operation, if the previous condition can already draw the final conclusion, all subsequent conditions are no longer executed! The logical operation here refers to logic and logic or.

We have to understand that the logic and the two conditions are true, it is true, if the first one is false, then the next one will not be executed. Logic is not one of the two conditions is true, then the result is true, so as long as the first is true, then the result is true, the following statement will not be executed. Then the following example:

Console.log (2&&3);    // 3console.log (2| | 3);    // 2Console.log (0&&1);    // 0console.log (0| | 1);    // 1

2. Passing by value

When assigning or passing a parameter to a function between two variables, the value of the original variable is copied to the other, modified one, and the other does not change. (This is to be likened to Voldemort's seven Horcrux, but do not know that every destruction of a Horcrux, Voldemort will be affected?) Harry Potter brain Remnant powder. )。

var a = ten; var b = a;a+ +; Console.log (b)   ; // Ten

But the value of the reference type is passed, only the reference address is passed, so one of the values is changed, and the other one changes. It was like a room with two keys, one opening the door, taking all the money, and the other key opening the door, the money was gone.

var a=[1,2,3,4]; var b=A;a.pop (); Console.log (b);    //  the

3. function declaration in advance

All Var-declared variables and function-declared functions are centrally declared at the top of the current scope prior to the formal execution of the program. But the assignment stays in place.

functionreturn 1;   } Console.log (Fun ());     // 2 function  return 2;   } Console.log (Fun ());     // 2 var fun=100; Console.log (Fun ());      // Error

  

JavaScript Novice Learning Notes 4--I can't remember a few pits: short-circuit logic, passing by value, declaring ahead

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.