The spread operator ..., introduced first in ES6 became one of the most popular and favourite feature among the developers.
A separate RFC was made for this much widely accepted feature to extend its functionalities to objects, prior it only worked on arrays.
This tutorial describes how to add element to the ending of an array using spread operator in ES6
Example with animals emoji
let animals = ["๐ฆ", "๐ต", "๐", "๐ฆ", "๐ฏ"];
console.log(animals);
// Output โ ["๐ฆ", "๐ต", "๐", "๐ฆ", "๐ฏ"]
console.log(animals.length);
// Output โ 5
animals = [...animals, "๐ฆ"];
console.log(animals.length);
// Output โ 6
console.log(animals);
// Output โ ["๐ฆ", "๐ต", "๐", "๐ฆ", "๐ฏ", "๐ฆ"]
// now with multiple elements
animals = [...animals, "๐", "๐ฆ", "๐ผ", "๐ฆ"];
console.log(animals.length);
// Output โ 10
console.log(animals);
// Output โ ["๐ฆ", "๐ต", "๐", "๐ฆ", "๐ฏ", "๐ฆ", "๐", "๐ฆ", "๐ผ", "๐ฆ"]
Happy coding
Related Articles
Deepen your understanding with these curated continuations.
Convert array to an object in JavaScript
This article explains simplest and quickest way to convert array to an object in JavaScript. Using widely accepted spread operator `...` makes easy to do it.
How to Update an Array Element in JavaScript
Learn the simplest ways to update array elements in JavaScript. This guide explains how to use assignment operators and modern methods to modify array values.
Flatten an Array One Level Deep in Javascript
This article explains to flatten an array one level deep in javascript comparing with lodash _.flatten method.