0 votes
1 view
in Web Technology by (26.9k points)

How do I append an object (such as a string or number) to an array in JavaScript?

1 Answer

0 votes
by (65.9k points)
edited by

There are many ways to append something to an array some of them I am mentioning here:-

You can use the Array.prototype.push method to append values to an array below is the code for the same:-

var arr = [ "Hi", "Hello", "Hey" ]; 

arr.push("Hiii");

console.log(arr);

image

Another thing you can do if you want to add the items of one array to another array, you can use firstArray.concat(secondArray)below is the code for it:-

var arr = [ "apple", "banana", "cherry" ]; 

arr = arr.concat([ "dragonfruit", "elderberry", "fig" ]);

console.log(arr);

image

...