JavaScript: String Constructor

By Xah Lee. Date: . Last updated: .

String() → returns a empty string primitive. Equivalent to "".

String(value) → convert value to string primitive.

new String(value) → like String (value), but returns a string object. (not string primitive. The practical difference is that with string object, it can store properties.)

「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()」

"use strict"

let s0 = "xyz";

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

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

// this is error when in strict mode
// s1.x = 3;
// TypeError: Cannot create property 'x' on string 'xyz'

// this is ok, because s2 is a object
s2.x = 3;

console.log ( Reflect.ownKeys ( s2 ) ); // [ '0', '1', '2', 'length', 'x' ]

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

String Topic

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

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

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

RegExp Topic

  1. JavaScript: RegExp Tutorial
  2. JavaScript: String Methods for RegExp
  3. JavaScript: RegExp Object
  4. JavaScript: RegExp Constructor
  5. JavaScript: RegExp.prototype
  6. JavaScript: RegExp Syntax
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.