Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

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

1 Answer

0 votes
by (106k 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

Related questions

0 votes
1 answer
0 votes
1 answer

Browse Categories

...