Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

String() constructor

BaselineWidely available

TheString() constructor createsString objects. When called as a function, it returns primitive values of type String.

Syntax

js
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 returnsvaluecoerced 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:

js
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.

js
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
js
const sym = Symbol("example");String(sym); // "Symbol(example)"

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-string-constructor

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp