new String(thing) String(thing)
Anything to be converted to a string.
String is called as a function, it coerces the parameter to a string primitive. When
String is called as a constructor (with
new), it creates a
String object, which is not a primitive.
Warning: You should rarely find yourself using
String as a constructor.
String function and String constructor produce different results:
const a = new String("Hello world"); // a === "Hello world" is false const b = String("Hello world"); // b === "Hello world" is true a instanceof String; // is true b instanceof String; // is false typeof a // "object" typeof b // "string"
Here, the function produces a string (the primitive type) as promised. However, the constructor produces an instance of the type String (an object wrapper) and that's why you rarely want to use the String constructor at all.
|ECMAScript Language Specification |
BCD tables only load in the browser