Array.prototype.toLocaleString()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
toLocaleString()
메서드는 Array
인스턴스의 배열의 요소를 나타내는
문자열을 반환합니다.
각 요소는 자체 toLocaleString
메서드를 사용하여 문자열로 변환되며, 이러한 문자열은 쉼표(",")와 같은 로케일별 구분 문자열로
분리됩니다.
시도해 보기
const array1 = [1, "a", new Date("21 Dec 1997 14:12:00 UTC")];
const localeString = array1.toLocaleString("en", { timeZone: "UTC" });
console.log(localeString);
// 예상 결과 값: "1,a,12/21/1997, 2:12:00 PM",
// 이것은 "en" 로케일과 UTC 시간대를 기반으로 합니다. 따라서 결과 값이 다를 수 있습니다.
구문
toLocaleString()
toLocaleString(locales)
toLocaleString(locales, options)
매개변수
locales
Optional-
BCP 47 언어 태그를 포함하는 문자열 또는 문자열 배열.
locales
인자의 일반적인 형식과 해석에 대한 내용은Intl
매개변수 설명 메인 페이지에서 확인하세요. options
Optional-
구성 속성을 포함하는 객체. 여기에서 전달할 수 있는 값은 변환되는 요소에 따라 다릅니다. 예를 들어, 숫자의 경우
Number.prototype.toLocaleString()
를 참고하세요.
반환 값
배열의 요소를 표현하는 문자열.
설명
Array.prototype.toLocaleString
메서드는 제공된 locales
및 options
매개변수를 사용하여 각 요소에서 toLocaleString
메서드를 호출하면서 요소를 순회합니다. 그리고 쉼표와 같은 구현에 따라 정의된 구분 문자를 사용하여 반환된 문자열을 연결합니다. 이 메서드는 두 개의 매개변수를 직접 사용하지 않으며, 단순히 각 요소의 toLocaleString()
메서드로 전달합니다. 구분 문자열의 선택은 locales
매개변수가 아닌 호스트의 현재 로케일에 따라 결정됩니다.
요소가 undefined
, null
이면, "null"
이나 "undefined"
대신 빈 문자열로 변환됩니다.
희소 배열에서 toLocaleString()
메서드는 빈 슬롯을 undefined
값을 가지고 있는 것처럼 처리하며 반복합니다.
toLocaleString()
메서드는 제네릭입니다. this
값이 length
속성과 정수 키 요소를 가지고 있으면 사용할 수 있습니다.
예제
로케일과 속성 사용
배열의 요소는 toLocaleString
메서드를 사용하여 문자열로 변환됩니다. 예를 들어, 이 코드에서는 prices
배열의 문자열과 숫자에 대해 자동으로 Number.prototype.toLocaleString()
메서드를 호출하여 통��� 형식으로 표시합니다.
const prices = ["¥7", 500, 8123, 12];
prices.toLocaleString("ja-JP", { style: "currency", currency: "JPY" });
// "¥7,¥500,¥8,123,¥12"
희소 배열에서 toLocaleString() 사용
toLocaleString()
은 빈 슬롯을 undefined
와 동일하게 처리하며 추가적인 구분 문자를 생성합니다.
console.log([1, , 3].toLocaleString()); // '1,,3'
배열이 아닌 객체에서 toLocaleString() 사용
toLocaleString()
메서드는 this
의 length
속성을 확인하고 length
보다 작은 음수가 아닌 정수를 키로 가진 속성에 접근합니다.
const arrayLike = {
length: 3,
0: 1,
1: 2,
2: 3,
3: 4, // length가 3이므로 toLocaleString()에 의해 무시됩니다
};
console.log(Array.prototype.toLocaleString.call(arrayLike));
// 1,2,3
명세서
Specification |
---|
ECMAScript® 2026 Language Specification # sec-array.prototype.tolocalestring |
ECMAScript® 2026 Internationalization API Specification # sup-array.prototype.tolocalestring |