JS: String Constructor

By Xah Lee. Date: . Last updated: .

String() → return a empty string, same as "".

String(value) → convert value to string primitive value, return it.

new String(value) → like String (value), but returns a string object.

“String(…)” Example

Calling String() without new is used for converting values to string primitive values.

Converting Primitive Values to Primitive Strings

console.log ( String () === "" ); // true

console.log ( String (undefined) === "undefined" ); // true
console.log ( String (null)  === "null" );          // true

console.log ( String (true) === "true" );   // true
console.log ( String (false) === "false" ); // true

console.log ( String (0) === "0" );      // true
console.log ( String (+0) === "0" );      // true
console.log ( String (-0) === "0" );      // true

console.log ( String (12) === "12" );      // true
console.log ( String (12.3) === "12.3" );  // true
console.log ( String (NaN) === "NaN" ); // true
console.log ( String (Infinity) === "Infinity" ); // true

console.log ( String ( Symbol() ) === "Symbol()" ); // true
console.log ( String ( Symbol("xyz") ) === "Symbol(xyz)" ); // true

Convert Object to String Primitive

// convert array to string
console.log ( String([3,4,5]) === "3,4,5" ); // true
// convert object to string
console.log ( String({"k":4}) === "[object Object]" ); // true

When String (obj) is called, and obj is type object, the result is the value of calling the function obj[Symbol.toPrimitive]() if this method exist, or obj.toString(), or obj.valueOf(), in that order.

// showing String(myArray) is the same as myArray.toString ()

const ar = [3,4,5];

console.log ( Reflect.has ( ar , Symbol.toPrimitive ) ); // false
console.log ( Reflect.has ( ar , "toString" ) ); // true

console.log ( String(ar) === ar.toString () ); // true
// showing String(myObj) is the same as myObj.toString ()

const obj = {"p":4};

console.log ( Reflect.has ( obj , Symbol.toPrimitive ) ); // false
console.log ( Reflect.has ( obj , "toString" ) ); // true

console.log ( String(obj) === obj.toString () ); // true

“new String (…)” Example

Calling new String (…) is same as String (…), except that the returned value is a string object.

Note: new String(…) is rarely useful.

Here's the difference between String() and new String().

// difference between “String()” and “new String()”

let s0 = "xyz";

let s1 = String (s0);
let s2 = new String (s0);

console.log ( typeof s1 === "string" ); // true
console.log ( typeof s2 === "object" ); // true

When string is converted to object, each character becomes a property.

let xx = new String("abc") ;
console.log(typeof xx === "object"); // true
console.log(xx);  // [String: 'abc']
console.log ( Reflect.ownKeys ( xx ) ); // [ '0', '1', '2', 'length' ]

ECMAScript 2015 §Text Processing#sec-string-constructor-string-value

JS String

  1. String Overview
  2. Template String
  3. Char, Code Unit, Code Point
  4. String Escape Sequence
  5. Unicode Escape Sequence
  6. String to Number
  7. Encode URL, Escape String
  8. Format Number
  9. Source Code Encoding
  10. Allowed Characters in Identifier
  11. String Object
  12. String.prototype

RegExp Topic

  1. JS: RegExp Tutorial
  2. JS: RegExp Syntax
  3. JS: Regex Find/Replace
  4. JS: RegExp Object
  5. JS: RegExp Constructor
  6. JS: RegExp.prototype
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS in Depth
  4. JS Reference
  5. DOM
  6. SVG
  7. Web Dev Blog