Spread Operator

 Spread Operator


The spread operator allows you to expand an iterable (such as an array or a string) into individual elements. It is denoted by three dots (...).



const numbers = [1, 2, 3];

const newArray = [...numbers, 4, 5];


console.log(newArray); // Output: [1, 2, 3, 4, 5]


const str = 'Hello';

const charArray = [...str];


console.log(charArray); // Output: ['H', 'e', 'l', 'l', 'o']

In the example above, the spread operator is used to expand the numbers array and the str string into individual elements. These elements are then used to create a new array (newArray) and a new array of characters (charArray), respectively.

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...