HTML: Markup language
CSS: Styling language
JavaScript: Scripting language
Web APIs: Programming interfaces
All web technology
Learn web development
Discover our tools
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
View in English Always switch to English
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015년 7월.
String() 생성자는 String 객체를 생성합니다. 함수로 호출될 경우 String 형의 원시 값을 반환합니다.
String()
String
new String(thing) String(thing)
참고 : String()은 new와 함께 호출하거나 아니면 단독으로 호출할 수 있습니다. 하지만 효과는 다릅니다. 반환 값을 참고하시기 바랍니다.
new
thing
문자로 바꿀 모든 것.
String()이 함수로 호출되면 (new 없이) 문자열 원시형으로 강제로 변환된 값을 반환합니다. 특히 Symbol 값은 "Symbol(description)"으로 변환되며, 여기서 description은 심볼의 description입니다.
값
"Symbol(description)"
description
String()이 생성자로 호출되면(new와 함께) 특별한 기호 심볼처리 없이 value을 문자열 원시형으로 강제로 변환하고 원시형이 아닌 String 객체로 감싸서 반환합니다.
value
경고 : String 생성자를 사용하는 경우는 거의 없습니다.
String 함수와 String 생성자는 다른 결과를 생성합니다.
const a = new String("Hello world"); // a === "Hello world" 는 false입니다 const b = String("Hello world"); // b === "Hello world" 는 true 입니다 a instanceof String; // true b instanceof String; // false typeof a; // "object" typeof b; // "string"
여기서 함수는 약속한 대로 문자열(원시 값 유형)을 생성합니다. 그러나 생성자는 문자열(객체 래퍼) 유형의 인스턴스를 생성하므로 문자열 생성자를 사용하는 경우는 거의 없습니다.
String()은 심볼을 어떠한 에러 발생 없이 변환을 할 수 있는 유일한 방법입니다. 매우 명시적이기 때문이지요.
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)"
Enable JavaScript to view this browser compatibility table.