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);
// Expected output: "1,a,12/21/1997, 2:12:00 PM",
// This assumes "en" locale and UTC timezone - your results may vary
構文
toLocaleString()
toLocaleString(locales)
toLocaleString(locales, options)
引数
locales省略可-
BCP 47 言語タグの文字列か、その配列です。
locales引数の一般的な形式と解釈については、Intlメインページの引数の説明を参照してください。 options省略可-
設定プロパティのオブジェクトです。数値に関しては
Number.prototype.toLocaleString()を、日付に関してはDate.prototype.toLocaleString()を見てください。
返値
配列の要素を表す文字列です。
解説
Array.prototype.toLocaleString メソッドは、その内容を走査し、すべての要素に対して toLocaleString メソッドを、引数 locales と options を指定して呼び出し、実装で定義された区切り文字 (",") でその結果を連結したものを返します。このメソッド自身は、この 2 つの引数を使用せず、各要素に対する toLocaleString() の呼び出しで渡すだけであることに注意してください。区切り文字列の選択はホストの現在のロケールに依存し、 locales 引数は使用しません。
要素が undefined、null の場合、文字列 "null" または "undefined" の代わりに空文字列に変換されます。
疎配列で使用する場合、toLocaleString() メソッドは空のスロットを undefined という値があるかのように反復処理します。
toLocaleString() メソッドは汎用的です。このメソッドは this 値に length プロパティと整数キーのプロパティがあることだけを期待します。
例
locales と options の使用
配列の要素は、その toLocaleString メソッドを使用して文字列に変換されます。
Object:Object.prototype.toLocaleString()Number:Number.prototype.toLocaleString()Date:Date.prototype.toLocaleString()
prices 配列内の文字列と数値の通貨を常に表示します。
const prices = ["¥7", 500, 8123, 12];
prices.toLocaleString("ja-JP", { style: "currency", currency: "JPY" });
// "¥7,¥500,¥8,123,¥12"
それ以外の例については、 Intl.NumberFormat や Intl.DateTimeFormat のページを参照してください。
疎配列に対する 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 |