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