TypeScript Array pop()

In this chapter, we will explore the pop() method for arrays in TypeScript. This method is a built-in function that removes the last element from an array and returns that element. Understanding how to use pop() is useful for managing and modifying arrays, especially when you need to remove elements from the end of the array.

Table of Contents

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

1. Definition

The pop() method removes the last element from an array and returns that element. This method changes the length of the array.

2. Syntax

array.pop();

Parameters

The pop() method does not take any parameters.

Return Value

The method returns the removed element from the array. If the array is empty, it returns undefined.

3. Examples

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

Example 1: Basic Usage

In this example, we use pop() to remove the last element from an array of numbers.

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

Example 2: Using pop() on a String Array

In this example, we use pop() to remove the last element from an array of strings.

let fruits: string[] = ["apple", "banana", "mango"];
let lastFruit = fruits.pop();
console.log(lastFruit); // Output: "mango"
console.log(fruits);    // Output: ["apple", "banana"]

Example 3: Using pop() on an Empty Array

In this example, we use pop() on an empty array to see how it behaves.

let emptyArray: number[] = [];
let result = emptyArray.pop();
console.log(result); // Output: undefined
console.log(emptyArray); // Output: []

Example 4: Removing Objects from an Array

In this example, we use pop() to remove the last object from an array of objects.

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

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

let lastPerson = people.pop();
console.log(lastPerson); // Output: { name: 'Priya', age: 28 }
console.log(people);     // Output: [ { name: 'Ravi', age: 25 }, { name: 'Ankit', age: 30 } ]

Example 5: Using pop() in a Loop

In this example, we use pop() in a loop to empty an array.

let numbers: number[] = [1, 2, 3, 4, 5];
while (numbers.length > 0) {
  console.log(numbers.pop());
}
// Output:
// 5
// 4
// 3
// 2
// 1

4. Conclusion

In this chapter, we explored the pop() method for arrays in TypeScript, which is used to remove the last element from an array and return that element. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use pop() effectively can help in various array manipulation tasks in TypeScript, especially when managing and modifying arrays by removing elements from 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