JS: String Constructor
String()
-
Return a empty string, same as
""
.String() === ""
String(value)
-
Convert value to string
Primitive Value
, return it.
// list of String(primitiveValue) for all possible primitiveValue // undefined type console.log(String(undefined) === "undefined"); // null type console.log(String(null) === "null"); // boolean primitives console.log(String(true) === "true"); console.log(String(false) === "false"); // number primitives console.log(String(0) === "0"); console.log(String(+0) === "0"); console.log(String(-0) === "0"); console.log(String(12) === "12"); console.log(String(12.3) === "12.3"); // number primitives NaN and Infinity console.log(String(NaN) === "NaN"); console.log(String(Infinity) === "Infinity"); // symbol primitives console.log(String(Symbol()) === "Symbol()"); console.log(String(Symbol("xyz")) === "Symbol(xyz)");
if argument is Object Type , the result is the value of calling the function
obj[Symbol.toPrimitive]
if this method exist, orobj.toString()
, orobj.valueOf()
, in that order.// convert array to string const aa = [3, 4, 5]; console.log(String(aa) === "3,4,5"); // String(myArray) is the same as myArray.toString() console.log(String(aa) === aa.toString());
// convert object to string const jj = { "p": 4 }; console.log(String(jj) === "[object Object]"); // String(myObj) is the same as myObj.toString() console.log(String(jj) === jj.toString());
new String(value)
-
Like
String(value)
, but returns a Array-Like Object that represents the string. The property values are String Code Units of the string.const xx = "abc"; const yy = new String(xx); console.log(typeof yy === "object"); console.log(yy.length === 3); console.log(yy["0"] === "a"); console.log(yy["1"] === "b"); console.log(yy["2"] === "c");
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