In this Typescript tutorial, we will see the array reverse() method in typescript, and how to reverse an array in typescript with different examples. Here are the examples we are going to cover:
- Typescript reverse array
- Typescript reverse array not in place
- Typescript reverse iterate array
- Typescript reverse array sort
- Typescript reverse JSON array
- Typescript reverse loop array
New to array? check out Typescript array
Typescript reverse array
Here we will see what is a reverse method in typescript, also we will see a simple example, of how we can use reverse an array using the reverse method in typescript.
Reverse Method in Typescript
The reverse () in typescript is used to reverse the array and return an array, which is sorted, in reference to the original array.
Syntax
reverse()
For example, we have an array, and we will reverse the array using the reverse Method. In the app.ts file, write the below code:
var array= ["henry", "john","Michel"]
console.log("Reversed array is:" + array.reverse())
To compile the code, run the below command and see the result in the console.
ts-node app.ts

This is what reverse() is, and the syntax of the reverse method.
Typescript reverse array not in place
Here we will see how we can reverse the array, not in place and is not working.
With reverse(), we will reverse the array with mutation, which means when the reverse method is used on the array, it will return the reverse of the array without mutating or without creating a new array. So here we will see how we can reverse the array without mutating the original array. In simple words, we can say we will print a new reverse array.
In the app.ts file, write the below code:
var num = [1, 2, 3, 4, 5];
console.log(num)
var reverse_num = num.slice().reverse()
console.log("new array:" +reverse_num)
With the slice method, it will create a new array, and then with the reverse method, we reverse the array.
To compile the code, run the below command, and you can see the result in the console.
ts-node app.ts

This is an example of a Typescript reverse array not in place.
typescript reverse iterate array
Here we will see how we can use the reverse() to iterate an array in a reverse way in typescript.
As we know the foreach will iterate the array in the forward direction. To iterate through an array in the backward direction, we will use the reverse method.
In the app.ts file write the below code:
var num = [1, 2, 3, 4, 5];
num.slice().reverse()
.forEach(function(elem) {
console.log(elem);
});
To compile the code, run the below command and you can see the result in the console:
ts-node app.ts

This is an example typescript reverse iterate array.
Read TypeScript Date
Typescript reverse array sort
Here we will see how we can reverse the sorted array in typescript.
For example, we have an array of strings, first, we will sort the array in alphabetical order using sort(), then we will use reverse the array using the reverse method.
const array: string[] = ['ammy', 'celia', 'zeck', 'fany'];
const descSortedReveredArray = array.sort().reverse();
console.log(descSortedReveredArray);
To compile the code, run the below command and you can see the result in the console.
ts-node app.ts

This is an example of a typescript reverse array sort
Typescript reverse json array
Here we will see how we can reverse JSON array in typescript using the reverse method.
For example, we have a JSON array, and by applying the reverse method we will reverse the array in typescript.
In the app.ts file, write the below code:
var usaActors: any= [
{first_name: 'Robin', last_name: 'Williams'},
{first_name: 'Morgan', last_name: 'Freeman'},
{first_name: 'Tom', last_name: 'Hanks'},
{first_name: 'Bruise', last_name: 'Wilis'},
{first_name: 'Brad', last_name: 'Pit'},
]
var result= usaActors.reverse();
console.log(result);
To compile the code, run the below command and you can see the result in the console.
ts-node app.ts

This is an example of a Typescript reverse JSON array.
Read TypeScript Map
Typescript reverse loop array
Here we will see how we can see a reverse loop array in typescript using the reverse method.
For example, we will use object.key() and reverse method, in for each to get the reverse array.
The Object.keys() function returns an array of the enumerable string-keyed property names of a given object.
In the app.ts file, write the below code:
var num = [1, 2, 3, 4, 5];
Object.keys(num).reverse()
.forEach(function (index:any) {
console.log(num[index]);
});
To compile the code, run the below command and you can see the output in the console.
ts-node app.ts

This is an example of a Typescript reverse loop array.
Conclusion
In this typescript tutorial, we learned all about the reverse method, and how we can use the reverse method. Here is the example we cover:
- Typescript reverse array
- Typescript reverse array not in place
- Typescript reverse iterate array
- Typescript reverse array sort
- Typescript reverse JSON array
- Typescript reverse loop array
You may like the following typescript tutorials:
- Typescript array find
- Typescript filter array
- Typescript sort array
- Typescript type annotation
- Typescript Identifiers and Keywords
I am Bijay a Microsoft MVP (8 times –Â My MVP Profile) in SharePoint and have more than 15 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com