The reverse() method reverses the order of the elements in an array in place and returns the reference to the same array, with its elements reversed.
The syntax of Array.reverse() method is:
reverse(): T[]
This reverse() method of Array reverses the elements in an array.
Array.reverse() returns value of type T[]
.
The reverse() method can be used to reverse the order of elements in a numeric array.
For example,
arr
with numeric values [1, 2, 3, 4, 5].reverse()
to reverse the order of elements in the array.arr
to the console.const arr = [1, 2, 3, 4, 5];
arr.reverse();
console.log(arr);
[5, 4, 3, 2, 1]
The reverse() method can be used to reverse the order of elements in an array of strings.
For example,
arr
with string values ['apple', 'banana', 'cherry'].reverse()
to reverse the order of elements in the array.arr
to the console.const arr = ['apple', 'banana', 'cherry'];
arr.reverse();
console.log(arr);
['cherry', 'banana', 'apple']
The reverse() method can be used to reverse the order of elements in an array with mixed data types.
For example,
arr
with mixed values [1, 'apple', true, null].reverse()
to reverse the order of elements in the array.arr
to the console.const arr = [1, 'apple', true, null];
arr.reverse();
console.log(arr);
[null, true, 'apple', 1]
In this TypeScript tutorial, we learned about reverse() method of Array: the syntax and few working examples with output and detailed explanation for each example.