Open In App

TypeScript Array reverse() Method

Last Updated : 04 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The Array.reverse() method in TypeScript is used to reverse the elements of an array in place, modifying the original array. It rearranges the elements such that the first element becomes the last, the second element becomes the second-to-last, and so on.

Syntax:

array.reverse(); 

Parameter: This method does not accept any parameter. 

Return Value: This method returns the reversed single value of the array. 

The below examples illustrate the Array reverse() Method in TypeScript.

Example 1: In this example, we are reversing the order of elements in the arr array using the reverse() method.

TypeScript
// Driver code
let arr: number[] = [11, 89, 23, 7, 98]; 

// Use of reverse() method
let val: number[] = arr.reverse();
 

console.log(val); 

Output: 

[ 98, 7, 23, 89, 11 ]

Example 2: In this example the array is reversed twice using the reverse() method. First, [2, 5, 6, 3, 8, 9] becomes [9, 8, 3, 6, 5, 2], then it returns to the original order.

TypeScript
// Driver code
let arr: number[] = [2, 5, 6, 3, 8, 9]; 
let val: number[];

// Use of reverse() method
val = arr.reverse();  
val = val.reverse();  

// Printing
console.log(val); 

Output: 

[ 2, 5, 6, 3, 8, 9 ]

Next Article

Similar Reads