JavaScript TypedArray.prototype.toReversed() Method
Last Updated :
12 Feb, 2024
In JavaScript, Array.prototype.toReversed() method is used to reverse an array. It returns a new array that has elements in reverse order.
Syntax :
toReversed()
Parameter:
This method does not accept any parameter.
Return Value:
This method returns a newly formed reverse array.
Example: This example shows the reverse of an array by the use of the Array.prototype.toReversed() method.
Javascript
Uint8Array.prototype.toReversed = function () {
const Arrays = new Uint8Array( this .length);
for (let i = 0; i < this .length; i++) {
Arrays[i] = this [ this .length - 1 - i];
}
return Arrays;
};
const originalArray =
new Uint8Array([1, 2, 9, 4, 5]);
const Arrays =
originalArray.toReversed();
console.log( "Original Array:" , originalArray);
console.log( "Reversed Array:" , Arrays);
|
Output
Original Array: Uint8Array(5) [ 1, 2, 9, 4, 5 ]
Reversed Array: Uint8Array(5) [ 5, 4, 9, 2, 1 ]
Share your thoughts in the comments
Please Login to comment...