JS: String Constructor

By Xah Lee. Date: . Last updated: .

String() → returns 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
let str = String([3,4,5]);
console.log ( str === "3,4,5" ); // true
// convert object to string
let str = String({"k":4});
console.log ( str === "[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.

var 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

String Topic

  1. JS: Default Charset/Encoding
  2. JS: String is 16-Bit Unit Sequence
  3. JS: Unicode Character Escape Sequence
  4. JS: Allowed Characters in Identifier
  5. HTML: Allowed Characters in id Attribute
  6. HTML: Character Sets and Encoding
  7. HTML/XML Entity List

  1. JS: Template String
  2. JS: Convert String to Number
  3. JavaScript Encode URL, Escape String
  4. JS: Format Number
  5. JS: JSON Object

  1. JS: String Object
  2. JS: String.prototype
  3. JS: String Constructor

RegExp Topic

  1. JS: RegExp Tutorial
  2. JS: String Methods for RegExp
  3. JS: RegExp Object
  4. JS: RegExp Constructor
  5. JS: RegExp.prototype
  6. JS: RegExp Syntax
Like what you read? Buy JavaScript in Depth