To declare a string in Javascript, we can easily use the literal (literal) Syntax:
VaR str1 = 'Hello JavaScript! ';
VaR str2 = "Hello world! ";
Since everything in Javascript is an object, we can also create it using the new syntax:
VaR str3 = new string ("Hello JavaScript! ");
VaR str4 = new string ("Hello world! ");
The question is, are these two methods equivalent to creating strings?
Str1 = str3 // true
Typeof (str1) = 'string' // true
Typeof (str3) = 'object' // true
Typeof (str3.tostring () = 'string' // true
It can be seen that the new string and the literal string use = to be equal, but they belong to different types. In JavaScript, you can use = to compare whether two variables are strictly equal (not only the literal value is equal, but also the type is equal ). Str1 = str3 // false
Note that when you use = for determination, JavaScript will automatically convert the type, and sometimes produce strange results: 12 = '12' // true
12 = '12' // false
This is why the jslint recommended ====instead of ===is used for variable judgment.