W3cubDocs

/JavaScript

String.string

The String constructor is used to create a new String object. When called instead as a function, it performs type conversion to a primitive string, which is usually more useful.

Syntax

new String(thing)
String(thing)

Parameters

thing
Anything to be converted to a string.

Examples

String constructor and String function

String function and String constructor produce different results:

typeof String('Hello world'); // string
typeof new String('Hello world'); // object

Here, the function produces a string (the primitive type) as promised. However, the constructor produces an instance of the type String (an object wrapper) and that's why you rarely want to use the String constructor at all.

Specifications

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
String() constructor 1 12 1 3 3 1
Mobile
Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
String() constructor 1 18 4 10.1 1 1.0
Server
Node.js
String() constructor 0.1.100

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/String