To reverse an array in TypeScript, you can use the same approach as in JavaScript, using the built-in `reverse()` method.
In this example,
intArray
containing integers.reverse()
method on intArray
to reverse its elements in place.let intArray: number[] = [1, 2, 3, 4, 5];
intArray.reverse();
console.log('Original Array:', intArray);
console.log('Reversed Array:', intArray);
Original Array: [ 5, 4, 3, 2, 1 ] Reversed Array: [ 5, 4, 3, 2, 1 ]
In this example,
strArray
containing strings.reverse()
method on strArray
to reverse its elements in place.let strArray: string[] = ['apple', 'banana', 'cherry'];
strArray.reverse();
console.log('Original Array:', strArray);
console.log('Reversed Array:', strArray);
Original Array: [ 'cherry', 'banana', 'apple' ] Reversed Array: [ 'cherry', 'banana', 'apple' ]
In this tutorial, we learned How to Reverse an Array in TypeScript language with well detailed examples.