JavaScript Code

How to append an Element to Array?


Problem Statement

given an element and array, write a JavaScript program to append the element to array

Solution

to append given element to an array, use JavaScript array method push(). call push() on the array object and pass the element as argument

Program

append item element to the array fruits

let fruits = ['apple', 'banana', 'cherry'];
let item = 'mango';
fruits.push(item);
console.log(fruits);



copyright @2022