When executing new String() , the string primitive is not returned to you, but << 22>.
For all purposes and purposes, it acts as a primitive string , but there are times when it will not. For example:
var a = new String('body'); jQuery(a);
Also, when comparing things, a problem may arise. When you compare objects in JavaScript, this is only true if they are the same exact object, and not just the same value. Example:
var a = new String('test'); var b = new String('test'); var c = a; var d = 'test'; a === b;
You can use .valueOf() to convert a string object to a string primitive.
new String('test').valueOf() === 'test';
So, I highly recommend using var a = '' or var a = "" . As for single quotes and double quotes, there is no difference. Consider this example:
var a = "My name is Joe O'Ryan"; var b = 'My name is Joe O\'Ryan';
So you need to use ' or " , but I suggest those over new String() .
Rocket hazmat
source share