JavaScript | Symbol.isConcatSpreadable symbol

The Symbol.isConcatSpreadable is a well-known symbol used to configure if a given object should be flattened to its array elements while using the Array.prototype.concat() method.

Syntax:

Array[Symbol.isConcatSpreadable]

Here Array is the array object which are to be flattened to its array elements.



Parameters: This symbol does not accept any parameter.

Return value: This symbol does not return any value.

JavaScript code to show the working of this function.
Example-1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
   // Creating some arrays
   const Array1 = [1, 2, 3];
   const Array2 = [4, 5, 6];
  
   // Calling concat() function
   let Array3 = Array1.concat(Array2);
  
   // Printing the concatenated array
   console.log(Array3);
  
   // Calling Symbol.isConcatSpreadable symbol
   Array2[Symbol.isConcatSpreadable] = false;
   Array3 = Array1.concat(Array2);
  
   // Printing the concatenated array
   // after calling of Symbol.isConcatSpreadable symbol
   console.log(Array3);
</script>

chevron_right


Output:

> Array [1, 2, 3, 4, 5, 6]
> Array [1, 2, 3, Array [4, 5, 6]]

Example-2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
   // Creating some arrays
   const Array1 = [1, 2, 3];
   const Array2 = [4, 5, 6];
  
   // Calling concat() function
   let Array3 = Array1.concat(Array2);
  
   // Printing the concatenated array
   console.log(Array3);
  
   // Calling Symbol.isConcatSpreadable symbol
   Array2[Symbol.isConcatSpreadable] = true;
   Array3 = Array1.concat(Array2);
  
   // Printing the concatenated array
   // after calling of Symbol.isConcatSpreadable symbol
   console.log(Array3);
</script>

chevron_right


Output:

> Array [1, 2, 3, 4, 5, 6]
> Array [1, 2, 3, 4, 5, 6]

Supported Browsers:

  • Google Chrome 48
  • Firefox 48
  • Edge 15
  • Opera
  • Apple Safari

Reference: https://devdocs.io/javascript/global_objects/symbol/isconcatspreadable



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.