In JavaScript, you can use the push()
method to add an element to the end of an array. Here’s how it works:
First, create an array (let’s call it fruits
):
const fruits = ["Banana", "Orange", "Apple", "Mango"];
To add a new item (let’s say “Kiwi”) to the end of the array, use the push()
method:
fruits.push("Kiwi");
If you want to add multiple items (for example, “Kiwi” and “Lemon”), you can pass them as separate arguments to push()
:
fruits.push("Kiwi", "Lemon");
The push()
method modifies the original array and returns the new length of the array.
So, in summary:
array.push(item1, item2, ..., itemX)
Remember that push()
is a handy way to dynamically update arrays in JavaScript.
It can be done using push() methOD
let arrayin=[1,2,3,4];
arrayin.push(5);
output:1,2,3,4,5
To push an element to an array in JavaScript, we can use the push() method. The push() method adds one or more elements to the end of an array and returns the new length of the array.
Example :
const numbers = [1, 2, 3]; numbers.push(4); console.log(numbers); // Output: [1, 2, 3, 4] numbers.push(5, 6); console.log(numbers); // Output: [1, 2, 3, 4, 5, 6]
let array =[ "A", "B", "C", "D"];
array. push ("E");
// output = A, B, C, D, E