TypeScript Array push()

In this chapter, we will explore the push() method for arrays in TypeScript. This method is a built-in function that adds one or more elements to the end of an array and returns the new length of the array. Understanding how to use push() is useful for managing and modifying arrays, especially when you need to add elements to the end of the array.

Table of Contents

  1. Definition
  2. Syntax
  3. Examples
  4. Conclusion

1. Definition

The push() method adds one or more elements to the end of an array and returns the new length of the array.

2. Syntax

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

Parameters

  • element1, element2, ..., elementN: The elements to add to the end of the array.

Return Value

The method returns the new length of the array after the elements have been added.

3. Examples

Let's look at some examples to understand how push() works in TypeScript.

Example 1: Basic Usage

In this example, we use push() to add a single element to the end of an array.

let numbers: number[] = [1, 2, 3];
let newLength = numbers.push(4);
console.log(newLength); // Output: 4
console.log(numbers);   // Output: [1, 2, 3, 4]

Example 2: Adding Multiple Elements

In this example, we use push() to add multiple elements to the end of an array.

let numbers: number[] = [1, 2, 3];
let newLength = numbers.push(4, 5, 6);
console.log(newLength); // Output: 6
console.log(numbers);   // Output: [1, 2, 3, 4, 5, 6]

Example 3: Using push() with Strings

In this example, we use push() to add elements to an array of strings.

let fruits: string[] = ["apple", "banana"];
let newLength = fruits.push("mango", "orange");
console.log(newLength); // Output: 4
console.log(fruits);    // Output: ["apple", "banana", "mango", "orange"]

Example 4: Using push() with Objects

In this example, we use push() to add objects to an array of objects.

interface Person {
  name: string;
  age: number;
}

let people: Person[] = [
  { name: "Ravi", age: 25 },
  { name: "Ankit", age: 30 }
];

let newPerson: Person = { name: "Priya", age: 28 };
let newLength = people.push(newPerson);
console.log(newLength); // Output: 3
console.log(people);    // Output: [ { name: 'Ravi', age: 25 }, { name: 'Ankit', age: 30 }, { name: 'Priya', age: 28 } ]

Example 5: Using push() in a Loop

In this example, we use push() in a loop to add elements to an array.

let numbers: number[] = [];
for (let i = 1; i <= 5; i++) {
  numbers.push(i);
}
console.log(numbers); // Output: [1, 2, 3, 4, 5]

4. Conclusion

In this chapter, we explored the push() method for arrays in TypeScript, which is used to add one or more elements to the end of an array and return the new length of the array. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use push() effectively can help in various array manipulation tasks in TypeScript, especially when managing and modifying arrays by adding elements to the end.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare