Javascript strings are copied and transmitted by reference, and compared by value. _ javascript tips-js tutorial

Source: Internet
Author: User
Strings are copied and transmitted by reference, but compared by value. When copying or passing by value, a space is allocated in the computer memory and the original value is copied to it, for more information, see Compare by value and by reference
Values of the Numbers and Boolean types (true and false) are copied, transmitted, and compared by value. When copying or passing by value, a space will be allocated in the computer memory and the original value will be copied to it. Then, even if you change the original value, the copied value is not affected (in turn), because these two values are independent entities.

Objects, arrays, and functions are copied, transferred, and compared by reference. When copying or passing by address, you actually create a pointer to the original item and use the pointer just like copying. If you subsequently change the original item, both the original item and the copy item will be changed (the same way in turn ). In fact, there is only one entity. A "copy" is not a real copy, but a reference to the data.

When comparing by reference, to make the comparison successful, the two variables must refer to the exact same entity. For example, two different Array objects are not equal even if they contain the same elements. To be successful, one of the variables must be another reference. To check whether two arrays contain the same elements, compare the results of the toString () method.

Finally, strings are copied and transmitted by reference, but compared by value. Note that if there are two String objects (created using new String ("something"), compare them by reference. However, if either or both of them are String values, compare them by value.

Strings are copied and transmitted by reference, but compared by value. Note that if there are two String objects (created using new String ("something"), compare them by reference. However, if either or both of them are String values, compare them by value.

The Code is as follows:


Var str1 = "aa ";
Var str2 = new String ("aa ");
Var str3 = str2;
Function test (p ){
Var str4 = p;
Console. log (str4 === str2 );
}
Console. log (str1 === str2); // false
Console. log (str3 === str2); // true
Test (str1); // false
Test (str2); // true

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.