What does .push do in javascript?

Software
AffiliatePal is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

Listen

Introduction

In JavaScript, the .push method is commonly used to add new elements to the end of an array. It allows developers to dynamically update arrays by appending values without having to manually specify the index position. This article will delve into the details of what the .push method does in JavaScript and explore its functionality and usage.

Understanding the .push Method

The .push method is a built-in JavaScript function that belongs to the Array object. It modifies the original array by adding one or more elements to the end. The syntax for using the .push method is as follows:

array.push(element1, element2, ..., elementN)

Here, array refers to the array to which the elements will be added, and element1, element2, ..., elementN represent the new elements to be appended.

Adding Elements to an Array

The primary purpose of the .push method is to add elements to an array. When using .push, the new elements are added to the end of the array, increasing its length by the number of elements pushed. For example:

let fruits = ['apple', 'banana', 'orange'];
fruits.push('grape', 'kiwi');
console.log(fruits);

The output will be: ['apple', 'banana', 'orange', 'grape', 'kiwi']

In this example, the .push method adds the elements ‘grape’ and ‘kiwi’ to the existing array, resulting in a new length of 5.

Returning the New Length

The .push method also returns the new length of the array after the elements have been added. This can be useful if you need to keep track of the array’s size. For instance:

let numbers = [1, 2, 3];
let newLength = numbers.push(4, 5);
console.log(newLength);

The output will be: 5

In this example, the .push method returns the new length of the array, which is then assigned to the variable newLength.

Adding a Single Element vs. Multiple Elements

The .push method can add a single element or multiple elements to an array. When adding a single element, you can simply pass it as an argument to the .push method. However, if you want to add multiple elements, you can separate them with commas within the method call. For example:

let animals = ['cat', 'dog'];
animals.push('elephant');
console.log(animals);

The output will be: ['cat', 'dog', 'elephant']

Alternatively, you can add multiple elements at once:

let animals = ['cat', 'dog'];
animals.push('elephant', 'giraffe', 'lion');
console.log(animals);

The output will be: ['cat', 'dog', 'elephant', 'giraffe', 'lion']

Conclusion

The .push method in JavaScript is a powerful tool for adding elements to the end of an array. It allows developers to dynamically update arrays by appending new values without the need to manually specify the index position. By using the .push method, you can easily modify arrays and keep track of their length.

References

– developer.mozilla.org: Array.prototype.push – https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push
– w3schools.com: JavaScript Array push() Method – https://www.w3schools.com/jsref/jsref_push.asp