JS: 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 JS: String Constructor
JavaScript, String
- JS: String Overview
- JS: Quote String
- JS: Template String
- JS: String Escape Sequence
- JS: Unicode Escape Sequence
- JS: String Operations
- JS: Iterate String
- JS: String Code Unit
- JS: Count Chars in String 🚀
- JS: Tagged Template String
- JS: Regex Functions
- JS: Convert String, Number
- JS: String Object
- JS: String Constructor
- JS: String.prototype