String() constructor
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
TheString()
constructor createsString
objects. When called as a function, it returns primitive values of type String.
Syntax
new String(thing)String(thing)
Note:String()
can be called with or withoutnew
, but with different effects. SeeReturn value.
Parameters
thing
Anything to be converted to a string.
Return value
WhenString()
is called as a function (withoutnew
), it returnsvalue
coerced to a string primitive. Specially,Symbol values are converted to"Symbol(description)"
, wheredescription
is thedescription of the Symbol, instead of throwing.
WhenString()
is called as a constructor (withnew
), it coercesvalue
to a string primitive (without special symbol handling) and returns a wrappingString
object, which isnot a primitive.
Warning:You should rarely find yourself usingString
as a constructor.
Examples
String constructor and String function
String function and String constructor produce different results:
const a = new String("Hello world"); // a === "Hello world" is falseconst b = String("Hello world"); // b === "Hello world" is truea instanceof String; // is trueb instanceof String; // is falsetypeof a; // "object"typeof b; // "string"
Here, the function produces a string (theprimitive type) as promised.However, the constructor produces an instance of the type String (an object wrapper) andthat's why you rarely want to use the String constructor at all.
Using String() to stringify a symbol
String()
is the only case where a symbol can be converted to a string without throwing, because it's very explicit.
const sym = Symbol("example");`${sym}`; // TypeError: Cannot convert a Symbol value to a string"" + sym; // TypeError: Cannot convert a Symbol value to a string"".concat(sym); // TypeError: Cannot convert a Symbol value to a string
const sym = Symbol("example");String(sym); // "Symbol(example)"
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-string-constructor |
Browser compatibility
See also
- Numbers and strings guide