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.
new String(thing) String(thing)
thing
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.
Desktop | ||||||
---|---|---|---|---|---|---|
String() constructor |
1 | 12 | 1 | 3 | 3 | 1 |
Mobile | ||||||
---|---|---|---|---|---|---|
String() constructor |
1 | 18 | 4 | 10.1 | 1 | 1.0 |
Server | |
---|---|
String() constructor |
0.1.100 |
© 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