JavaScript: Difference Between String() vs new String()
both converts any value to a string. one returns a string primitive, the other returns a string object.
String(arg)
return a Primitive Value. The result is type"string"
.new String(arg)
return a Array-Like Object, with integer keys, and values of String Code Unit (characters if the arg is all ASCII Characters) The result is type"object"
.
const xx = "abc"; const yy = String(xx); console.log(yy === xx); console.log(typeof yy === "string");
const xx = "abc"; const yy = new String(xx); console.log(typeof yy === "object"); console.log(Array.isArray(yy) === false);
back to JavaScript: String Constructor