• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
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.  How to Remove Decimals of a Number in JavaScript
  • 2.  Using Javascript to Check if Value is Not Undefined
  • 3.  JavaScript atan2 – Find Arctangent of the Quotient of Two Numbers
  • 4.  Using JavaScript to Compare Dates
  • 5.  How to Filter a List of Divs with a Text Input Bar Using Javascript
  • 6.  Remove All Instances of Value From an Array in JavaScript
  • 7.  Using JavaScript to Check if Array is Empty
  • 8.  How to Swap Values in an Array in JavaScript
  • 9.  Using JavaScript to Get the Current URL
  • 10.  Using JavaScript to Check if String Contains Only Letters

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 *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

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