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.
String() constructor
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 compatibility
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | Deno | Node.js | |
String |
1
|
12
|
1
|
3
|
3
|
1
|
1
|
18
|
4
|
10.1
|
1
|
1.0
|
1.0
|
0.10.0
|
See also
© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/String