The String
constructor is used to create a new String
object. It performs type conversion when called as a function, rather than as a constructor, 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
Specification |
---|
ECMAScript (ECMA-262) The definition of 'String constructor' in that specification. |
Browser compatibility
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
String() constructor | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
Legend
- Full support
- Full support