Home > Web Front-end > JS Tutorial > JavaScript Spread Operator

JavaScript Spread Operator

Susan Sarandon
Release: 2024-12-14 12:20:15
Original
615 people have browsed it

JavaScript Spread Operator

JavaScript Spread Operator (…)

  • The Spread Operator in JavaScript expands (unpacks) an array into its individual elements.

*It’s used with both arrays and objects, in arrays it’s used when:

1. Building an Array
2. passing arguments into a function

1- Building an Array:

  • in here we can use it to expand an array element and use it in another (array for example).
const arr = [5, 6, 7];

// without the spread operator ?

const badArr = [1, 2, 3, 4, arr[0], arr[1], arr[2]];
console.log(badArr); // [1, 2, 3, 4, 5, 6, 7]

// with the spread operator ?

const goodArr = [1, 2, 3, 4, ...arr];
console.log(goodArr); // [1, 2, 3, 4, 5, 6, 7]

Copy after login
Copy after login
  • as you can see, the spread operator makes things much easier.

  • if you again wanted the individual elements of the expanded array, use the spread operator:

console.log(...goodArr); // 1 2 3 4 5 6 7

//the line above is just like writing this code:

console.log(1, 2, 3, 4, 5, 6, 7); // 1 2 3 4 5 6 7
Copy after login
Copy after login
  • another example to understand more:
const foods = ['chicken', 'meat', 'rice'];

const Newfoods = [...foods, 'pizza '];
console.log(Newfoods); // ['chicken', 'meat', 'rice', 'pizza ']
Copy after login
  • keep in mind that here we are creating a completely new array, we are not changing (mutating) the foods array, look:
console.log(foods); // ['chicken', 'meat', 'rice']
Copy after login
  • Spread Operator is similar to Destructuring, but the difference is that the spread operator doesn’t create new variables, so we can use it only in places where we write values separated by commas.

Two useful use cases of the spread operator with arrays:

1.creating a copy of an array:

const arrOriginal = [1, 2, 3, 4, 5];

const arrCopy = [...arrOriginal];
console.log(arrCopy); // [1, 2, 3, 4, 5]
Copy after login

2.merging two or more arrays:

const arr1 = ['A', 'B', 'C'];
const arr2 = ['D', 'E', 'F'];

const mergedArr = [...arr1, ...arr2];
console.log(mergedArr); // ['A', 'B', 'C', 'D', 'E', 'F']
Copy after login
  • The spread Operator does not only work on arrays, it also works on all iterables, which are things like: arrays, strings, maps and sets (most of the built-in data structures), but NOT (objects).
  • on strings: each letter on the original string = an individual element, example:
const str = 'spongeBob';

const letters = [...str, 'squarePants'];
console.log(letters); // ['s', 'p', 'o', 'n', 'g', 'e', 'B', 'o', 'b', 'squarePants']
Copy after login
  • but remember, we still don’t want to use the spread operator other than in the two situations we specified at the top, which where “building an array” & “passing arguments”, because for example, if we wanted to use some values separated by commas to create a string by template literals, it won’t work and will give us an error, because it is not a place that expects multiple values separated by a comma:
console.log(`spelling sponge bob's name: ${...str}`);  // Expression expected

Copy after login

2- passing arguments into a function

const arr = [5, 6, 7];

// without the spread operator ?

const badArr = [1, 2, 3, 4, arr[0], arr[1], arr[2]];
console.log(badArr); // [1, 2, 3, 4, 5, 6, 7]

// with the spread operator ?

const goodArr = [1, 2, 3, 4, ...arr];
console.log(goodArr); // [1, 2, 3, 4, 5, 6, 7]

Copy after login
Copy after login
  • Using Spread Operator:
  • Starting with ES2018, The Spread Operator actually also works for objects, even though objects are not iterables, example:
console.log(...goodArr); // 1 2 3 4 5 6 7

//the line above is just like writing this code:

console.log(1, 2, 3, 4, 5, 6, 7); // 1 2 3 4 5 6 7
Copy after login
Copy after login
  • in the first one we created a new object with the same properties as the restaurant object, but we added another property which is the netWorth property.
  • in the second one, we made a copy of the restaurant object, note that Changes to the copied object do not affect the original object, and vice versa.

Hope you understood everything here, if you have any questions feel free to ask in the comments section, THANKS for reading ?

The above is the detailed content of JavaScript Spread Operator. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template