JavaScript strings are copied and passed by reference, compared by value introduction and application of _javascript techniques

Source: Internet
Author: User
comparisons by value and by reference
Numbers and Boolean values (True and false) are copied, passed, and compared by value. When copied or passed by value, a space is allocated in the computer's memory and the original value is copied to it. Then, even if you change the original value, the copied value (and vice versa) will not be affected, because the two values are separate entities.

objects, arrays, and functions are copied, passed, and compared by reference. When copied or delivered by address, you actually create a pointer to the original item, and then use the pointer just like a copy. If you subsequently change the original item, the original item and the copy item are also changed (and vice versa). There is actually only one entity; "Copy" is not a real copy, but just another reference to that data.

When compared by reference, to be successful, two variables must refer to the exact same entity. For example, two different Array objects will be less equal even if they contain the same elements. To be more successful, one of the variables must be a reference for another. To check whether two arrays contain the same elements, compare the results of the toString () method.

Finally, strings are copied and passed by reference, but are compared by value. Note that if you have two string objects (created with new string ("something"), compare them by reference, but if one or both are string values, compare them by value.

Strings are copied and passed by reference, but are compared by value. Note that if you have two string objects (created with new string ("something"), compare them by reference, but if one or both are string values, compare them by value.
Copy Code code 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 (s TR2);//true

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.