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() メソッドは、配列の要素を表す文字列を返します。配列の要素は、それぞれの toLocaleString メソッドを使い、ロケール固有の文字列に変換されます(例えばカンマ "," など)。

試してみましょう

構文

js
toLocaleString();
toLocaleString(locales);
toLocaleString(locales, options);

引数

locales 省略可

BCP 47 言語タグの文字列か、その配列です。locales 引数の一般的な形式と解釈については、ロケールの識別とネゴシエーションを参照してください。

options 省略可

設定プロパティのオブジェクトです。数値に関しては Number.prototype.toLocaleString() を、日付に関しては Date.prototype.toLocaleString() を見てください。

返値

配列の要素を表す文字列です。

解説

Array.prototype.toLocaleString メソッドは、その内容を走査し、すべての要素に対して toLocaleString メソッドを、引数 localesoptions を指定して呼び出し、実装で定義された区切り文字 (",") でその結果を連結したものを返します。このメソッド自身は、この 2 つの引数を使用せず、各要素に対する toLocaleString() の呼び出しで渡すだけであることに注意してください。区切り文字列の選択はホストの現在のロケールに依存し、 locales 引数は使用しません。

locales と options の使用

配列の要素は、その toLocaleString メソッドを使用して文字列に変換されます。

prices 配列内の文字列と数値の通貨を常に表示します。

js
const prices = ["¥7", 500, 8123, 12];
prices.toLocaleString("ja-JP", { style: "currency", currency: "JPY" });

// "¥7,¥500,¥8,123,¥12"

それ以外の例については、 Intl.NumberFormatIntl.DateTimeFormat のページを参照してください。

仕様書

Specification
ECMAScript Language Specification
# sec-array.prototype.tolocalestring
ECMAScript Internationalization API Specification
# sup-array.prototype.tolocalestring

ブラウザーの互換性

BCD tables only load in the browser

関連情報