The push() method adds one or more elements to the end of an array and returns the new length of the array.
The syntax of Array.push() method is:
push(...items: T[]): number
This push() method of Array appends new elements to an array, and returns the new length of the array.
Parameter | Optional/Required | Description |
---|---|---|
items | required | The elements to add to the end of the array. |
Array.push() returns value of type number
.
The push() method can be used to add multiple elements to the end of an array.
For example,
arr
with initial values [1, 2, 3].push()
to add the elements 4 and 5 to the end of the array.newLength
.arr
and the new length newLength
to the console.const arr = [1, 2, 3];
const newLength = arr.push(4, 5);
console.log(arr); // [1, 2, 3, 4, 5]
console.log(newLength); // 5
[1, 2, 3, 4, 5] 5
The push() method can be used to add a single element to the end of an array.
For example,
arr
with initial values ['a', 'b', 'c'].push()
to add the element 'd' to the end of the array.newLength
.arr
and the new length newLength
to the console.const arr = ['a', 'b', 'c'];
const newLength = arr.push('d');
console.log(arr); // ['a', 'b', 'c', 'd']
console.log(newLength); // 4
['a', 'b', 'c', 'd'] 4
The push() method can be used to add elements of different types to the end of an array.
For example,
arr
with initial values [1, 'apple', true].push()
to add the elements 2 and 'banana' to the end of the array.newLength
.arr
and the new length newLength
to the console.const arr = [1, 'apple', true];
const newLength = arr.push(2, 'banana');
console.log(arr); // [1, 'apple', true, 2, 'banana']
console.log(newLength); // 5
[1, 'apple', true, 2, 'banana'] 5
In this TypeScript tutorial, we learned about push() method of Array: the syntax and few working examples with output and detailed explanation for each example.