TypeScript Array reverse()

In this chapter, we will explore the reverse() method for arrays in TypeScript. This method is a built-in function that reverses the elements of an array in place. The first array element becomes the last, and the last array element becomes the first. Understanding how to use reverse() is useful for tasks that require the order of elements to be reversed.

Table of Contents

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

1. Definition

The reverse() method reverses the elements of an array in place. This method modifies the original array and returns the array with its elements reversed.

2. Syntax

array.reverse();

Parameters

The reverse() method does not take any parameters.

Return Value

The method returns the array with its elements reversed.

3. Examples

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

Example 1: Basic Usage

In this example, we use reverse() to reverse the elements of an array of numbers.

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

Example 2: Reversing a String Array

In this example, we use reverse() to reverse the elements of an array of strings.

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

Example 3: Using reverse() with Mixed Data Types

In this example, we use reverse() to reverse the elements of an array containing different data types.

let mixedArray: (number | string | boolean)[] = [1, "apple", true, 42, "banana"];
let reversedMixedArray = mixedArray.reverse();
console.log(reversedMixedArray); // Output: ["banana", 42, true, "apple", 1]
console.log(mixedArray);         // Output: ["banana", 42, true, "apple", 1]

Example 4: Reversing an Array of Objects

In this example, we use reverse() to reverse the elements of 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 reversedPeople = people.reverse();
console.log(reversedPeople);
// Output:
// [ { name: 'Priya', age: 28 },
//   { name: 'Ankit', age: 30 },
//   { name: 'Ravi', age: 25 } ]
console.log(people);
// Output:
// [ { name: 'Priya', age: 28 },
//   { name: 'Ankit', age: 30 },
//   { name: 'Ravi', age: 25 } ]

Example 5: Reversing an Empty Array

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

let emptyArray: number[] = [];
let reversedEmptyArray = emptyArray.reverse();
console.log(reversedEmptyArray); // Output: []
console.log(emptyArray);         // Output: []

4. Conclusion

In this chapter, we explored the reverse() method for arrays in TypeScript, which is used to reverse the elements of an array in place. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use reverse() effectively can help in various array manipulation tasks in TypeScript, especially when the order of elements needs to be reversed.

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