loading...
Cover image for How to add an element to ending of an array with ES6

How to add an element to ending of an array with ES6

cooldashing24 profile image Vishnu Damwala Originally published at meshworld.in ใƒป1 min read

The spread operator ..., introduced first in ES6 became one of the most popular and favourite feature among the developers.

This tutorial describes how to add an 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 โ†’ ["๐Ÿฆ", "๐Ÿต", "๐Ÿ•", "๐ŸฆŠ", "๐Ÿฏ", "๐Ÿฆ„"]

Read the complete post on our site MeshWorld - How to add an element to ending of an array with ES6

Happy coding

Discussion

markdown guide