Is there a difference? Will string 2 inherit different object prototypes?
var s1 = 1234 + ''; var s2 = String(1234); //s1.someNewFunc(); error? //s2.someNewFunc();
var s1 = 1234 + '';
var s2 = String(1234);
The String() function also returns a primitive string literal. s2 will have the same members as s1 because they are both the same type.
var s3 = new String("1234");
Will create an object of type String rather than a primitive string literal. This does have different members and is of type object.
var s1 = 1234 + ''; var s2 = String(1234); typeof s1 //string typeof s2 //string
Both will behave the same way.
Also, there is a nice explanation about string primitives vs. objects here:
Distinction between string primitives and String objects
"abcde".substr(1,3); //bcd true.valueOf(); //true
©2020 All rights reserved.