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