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

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

1 Answer

0 votes
by (107k 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" ]; 




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" ]);



Welcome to Intellipaat Community. Get your technical queries answered by top developers !