Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Symbol isConcatSpreadable Property

JavaScript Symbol isConcatSpreadable Property

JavaScript 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 is to be flattened to its array elements.

Parameters: This symbol does not accept any parameter.

Return value: This symbol does not return any value.

Example 1: In this example, we will use isConcatSpreadable Property

javascript




// 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);


Output

[ 1, 2, 3, 4, 5, 6 ]
[ 1, 2, 3, [ 4, 5, 6, [Symbol(Symbol.isConcatSpreadable)]: false ] ]


Example 2: In this example, we will use isConcatSpreadable Property

javascript




// 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);


Output

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


Supported Browsers: 

  • Google Chrome 48 and above
  • Firefox 48 and above
  • Edge 15 and above
  • Opera 35 and above
  • Apple Safari 10 and above
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments