The push()
Method
The push()
method adds one or more elements to the end of an array and returns the new length of the array. This is one of the most commonly used methods for adding data to an array.
Example 1: Adding a single element
// Example of adding a single element to an array
const fruits = ['apple', 'banana'];
const newLength = fruits.push('orange');
console.log(fruits); // Output: ['apple', 'banana', 'orange']
console.log(newLength); // Output: 3
Explanation
In this example, we have an array fruits
. The push('orange')
method call adds the string 'orange' to the end of the fruits
array. The variable newLength
stores the returned value, which is the new size of the array (3).
Example 2: Adding multiple elements
// Example of adding multiple elements to an array
const vegetables = ['carrot', 'broccoli'];
vegetables.push('spinach', 'kale');
console.log(vegetables); // Output: ['carrot', 'broccoli', 'spinach', 'kale']
Explanation
The push()
method can accept multiple arguments. Here, we add both 'spinach' and 'kale' to the vegetables
array in a single call, and they are added in the order they were provided.
Example 3: Adding elements of different data types
// Example of adding different data types to an array
const mixedData = [1, 'hello'];
mixedData.push(true, { name: 'John' }, null);
console.log(mixedData); // Output: [1, 'hello', true, { name: 'John' }, null]
Explanation
JavaScript arrays can hold elements of various data types. This example demonstrates adding a boolean, an object, and a null value to the mixedData
array using push()
.
Example 4: Using push() to merge two arrays
// Example of merging one array into another using push()
const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
Array.prototype.push.apply(array1, array2);
console.log(array1); // Output: [1, 2, 3, 4, 5, 6]
Explanation
While concat()
is the standard for merging arrays, you can use push()
with apply()
to achieve a similar result. apply()
calls push()
on array1
, passing the elements of array2
as individual arguments, effectively merging array2
into array1
.
Example 5: Adding an array as a single element
// Example of adding an array as a single element
const numbers = [10, 20];
numbers.push([30, 40]);
console.log(numbers); // Output: [10, 20, [30, 40]]
Explanation
If you pass an array to push()
, the entire array is added as a single new element. In this case, [30, 40]
becomes the third element of the numbers
array, creating a nested array.
Example 6: Using push() in a loop
// Example of using push() within a for loop
const evenNumbers = [];
for (let i = 2; i <= 10; i += 2) {
evenNumbers.push(i);
}
console.log(evenNumbers); // Output: [2, 4, 6, 8, 10]
Explanation
This code snippet initializes an empty array evenNumbers
. The for
loop iterates from 2 to 10, and in each iteration, the current even number i
is added to the evenNumbers
array using push()
.
Example 7: Pushing return values from functions
// Example of pushing the result of a function call
function createId() {
return Math.floor(Math.random() * 1000);
}
const userIds = [101, 102];
userIds.push(createId());
console.log(userIds); // Output: [101, 102, <a random number between 0 and 999>]
Explanation
Here, the createId
function generates a random number. The return value of this function is directly passed as an argument to push()
, adding a new unique ID to the userIds
array.