DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

Cover image for How to add an element to ending of an array with ES6
Vishnu Damwala
Vishnu Damwala

Posted on • Originally published at meshworld.in

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

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

Top comments (0)

๐ŸŒš Browsing with dark mode makes you a better developer.

It's a scientific fact.