add new item in array in Javascript
Code snippet on how to add new item in array in Javascript
const fruits = ["apple", "banana", "strawberry"];
fruits.push("pear"); // add the string "pear" to the end of the array
This code adds a new item, “pear”, to the end of the existing array of fruits. The code first declares a variable - in this case, an array of strings named “fruits” - and assigns it three elements: “apple”, “banana”, and “strawberry”. The second line of code uses the array’s push() function to add the new element to the end of the array; in this case, the new element is “pear”. Line two appends the new element to the end of the existing array. Finally, the new array contains four elements: “apple”, “banana”, “strawberry”, and “pear”.