• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / JavaScript / Using JavaScript to Add to Array

Using JavaScript to Add to Array

September 20, 2022 Leave a Comment

In JavaScript, to add to an array, we can simply use the JavaScript Array push() method. The push() method will add an element to the end of the array.

var numArray = [1, 2, 3];

numArray.push(4);

console.log(numArray);

#Output:
[1, 2, 3, 4]

You can also use the push() method to add multiple elements to an array.

var numArray = [1, 2, 3];

numArray.push(4,5);

console.log(numArray);

#Output:
[1, 2, 3, 4, 5]

When working with collections of data in JavaScript, the ability to easily add items or change the collection is important.

One such case where you may want to modify a collection is if you want to add elements to an array in JavaScript.

To add on item to an array in JavaScript, you can use the push() method. The push() method will add an element or elements to the end of the array.

Below shows a simple example of how you can use the push() method to add an item to an array in JavaScript.

var numArray = [1, 2, 3];

numArray.push(4);

console.log(numArray);

#Output:
[1, 2, 3, 4]

Adding Multiple Items to an Array in JavaScript

If you want to add multiple items to an array in JavaScript, then you could use the push() method and include multiple items in it.

Below shows you how to add multiple elements to an array using the push() method in JavaScript.

var numArray = [1, 2, 3];

numArray.push(4,5,6,7);

console.log(numArray);

#Output:
[1, 2, 3, 4, 5, 6, 7]

Adding Multiple Items to an Array in JavaScript with the Spread Operator (…)

If you want to add multiple items to an array in JavaScript, then we could also use the Spread Operator (…).

Below shows you how to add multiple elements to an array using Spread Operator (…) in JavaScript.

var numArray = [1, 2, 3];

var arr1 = [4,5];
var combinedArray1 = [ ...numArray, ...arr1 ];

console.log(combinedArray1);

var arr2 = [6,7];
var combinedArray2 = [ ...combinedArray1, ...arr2 ];

console.log(combinedArray2);

#Output:
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6, 7]

Hopefully this article has been useful for you to learn how to use JavaScript to add to an array.

Other Articles You'll Also Like:

  • 1.  Demystifying Square Brackets: Understanding the [] Notation
  • 2.  Using JavaScript to Get the Page Title
  • 3.  How to Remove Non Numbers From String in JavaScript
  • 4.  Using JavaScript to Set the Cursor Position
  • 5.  outerHTML – How to Get and Change the outerHTML Property of an Element
  • 6.  React Axios Interceptor to Prevent Infinite Loops in JWT Authentication
  • 7.  Using JavaScript to Check if Variable is a String
  • 8.  How to Empty a String in JavaScript
  • 9.  Using JavaScript to get the Last Element in Array
  • 10.  Using the getElementsByClassName Method in JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · The Programming Expert · About · Privacy Policy