String() constructor
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
Specification |
---|
ECMAScript Language Specification # sec-string-constructor |
Browser compatibility
BCD tables only load in the browser