DEV Community

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

Vishnu Damwala
A human being currently living in India๐Ÿ‡ฎ๐Ÿ‡ณ on Earth. Founder of MeshWorld, a web geek, an industry experienced web developer & tutor/instructor currently lives in Surat City.
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 (0)