--- title: Array.prototype.toString() slug: Web/JavaScript/Reference/Global_Objects/Array/toString tags: - Array - JavaScript - Method - Prototype translation_of: Web/JavaScript/Reference/Global_Objects/Array/toString ---
toString()
메서드는 지정된 배열 및 그 요소를 나타내는 문자열을 반환합니다.
{{EmbedInteractiveExample("pages/js/array-tostring.html")}}
arr.toString()
배열을 표현하는 문자열을 반환합니다.
{{jsxref("Array")}} 객체는 {{jsxref("Object")}}의 toString
메서드를 재정의(override)합니다. Array 객체에 대해, toString
메서드는 배열을 합쳐(join) 쉼표로 구분된 각 배열 요소를 포함하는 문자열 하나를 반환합니다. 예를 들어, 다음 코드는 배열을 생성하며 그 배열을 문자열로 변환하기 위해 toString
을 사용합니다.
var monthNames = ['Jan', 'Feb', 'Mar', 'Apr']; var myVar = monthNames.toString(); // 'Jan,Feb,Mar,Apr'을 myVar에 할당.
JavaScript는 배열이 텍스트 값으로 표현되거나 배열이 문자열 연결(concatenation)에 참조될 때 자동으로 toString
메서드를 호출합니다.
JavaScript 1.8.5 (Firefox 4)부터, ECMAScript 제5판 의미(semantics)와 일치하는 toString()
메서드는 일반(generic) 메서드이므로 모든 객체에 사용될 수 있습니다. 객체가 join()
메서드가 있는 경우, 호출되어 그 값이 반환됩니다. 그렇지 않으면 {{jsxref("Object.prototype.toString()")}}가 호출되어 그 결과값이 반환됩니다.
명세 | 상태 | 설명 |
---|---|---|
{{SpecName('ES1')}} | {{Spec2('ES1')}} | 초기 정의. JavaScript 1.1에서 구현됨. |
{{SpecName('ES5.1', '#sec-15.4.4.2', 'Array.prototype.toString')}} | {{Spec2('ES5.1')}} | |
{{SpecName('ES6', '#sec-array.prototype.tostring', 'Array.prototype.toString')}} | {{Spec2('ES6')}} | |
{{SpecName('ESDraft', '#sec-array.prototype.tostring', 'Array.prototype.toString')}} | {{Spec2('ESDraft')}} |
{{Compat("javascript.builtins.Array.toString")}}