Symbol.isConcatSpreadable

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2015.

Symbol.isConcatSpreadable は静的データプロパティで、ウェルノウンシンボルである Symbol.isConcatSpreadable を表します。Array.prototype.concat() メソッドは、連結される各オブジェクトに対してこのシンボルを探し、配列風オブジェクトとして扱って配列要素を平坦化すべきかどうかを判断します。

試してみましょう

const alpha = ["a", "b", "c"];
const numeric = [1, 2, 3];
let alphaNumeric = alpha.concat(numeric);

console.log(alphaNumeric);
// Expected output: Array ["a", "b", "c", 1, 2, 3]

numeric[Symbol.isConcatSpreadable] = false;
alphaNumeric = alpha.concat(numeric);

console.log(alphaNumeric);
// Expected output: Array ["a", "b", "c", Array [1, 2, 3]]

ウェルノウンシンボル Symbol.isConcatSpreadable です。

Symbol.isConcatSpreadable のプロパティ属性
書込可能不可
列挙可能不可
設定可能不可

解説

[Symbol.isConcatSpreadable] プロパティは、直接または継承されたプロパティとして定義でき、その値は論理値です。これが配列や配列風オブジェクトの挙動を制御できます。

  • 配列オブジェクトでは、既定の動作は要素の展開(平坦化)です。Symbol.isConcatSpreadable により、これらの場合に平坦化を避けることができます。
  • 配列風オブジェクトでは、既定の動作は展開や平坦化を行いません。Symbol.isConcatSpreadable により、これらの場合に平坦化を強制することができます。

配列

既定で、Array.prototype.concat() は配列を次の結果のように展開(平坦化)します。

js
const alpha = ["a", "b", "c"];
const numeric = [1, 2, 3];

const alphaNumeric = alpha.concat(numeric);

console.log(alphaNumeric); // 結果: ['a', 'b', 'c', 1, 2, 3]

Symbol.isConcatSpreadablefalse に設定すると、既定の動作を無効にすることができます。

js
const alpha = ["a", "b", "c"];
const numeric = [1, 2, 3];

numeric[Symbol.isConcatSpreadable] = false;
const alphaNumeric = alpha.concat(numeric);

console.log(alphaNumeric); // 結果: ['a', 'b', 'c', [1, 2, 3] ]

配列風オブジェクト

配列風オブジェクトは、既定で展開されません。平坦化された配列を取得するには、Symbol.isConcatSpreadabletrue に設定する必要があります。

js
const x = [1, 2, 3];

const fakeArray = {
  [Symbol.isConcatSpreadable]: true,
  length: 2,
  0: "hello",
  1: "world",
};

x.concat(fakeArray); // [1, 2, 3, "hello", "world"]

メモ: length プロパティは、追加するオブジェクトプロパティの数を制御するために使用されます。上記の例では、length:2 は 2 つのプロパティを追加する必要があることを示しています。

仕様書

Specification
ECMAScript® 2025 Language Specification
# sec-symbol.isconcatspreadable

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
isConcatSpreadable

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報