JavaScript Array reverse() Definition
The reverse () method reverses the array. The first element of the array becomes the last element and vice versa.
Syntax:
array.reverse()
JavaScript Array reverse() Examples
Example 1
let list = ["a", "b", "c", "d"];
console.log("Normal Array: " + list);
let newList = list.reverse();
console.log("Reverse Array: "+ newList)
Output:
Normal Array: a,b,c,d
Reverse Array: d,c,b,a
You can define the reverse () function yourself
function reverseArr(input) {
var ret = new Array;
for (var i = input.length - 1; i >= 0; i--) {
ret.push(input[i]);
}
return ret;
}
Example:
var x = ["a","b","c","d"]
var y= reverseArr(a);
console.log(x)
console.log(y)
[“a”,”b”,”c”,”d”]
[“d”,”c”,”b”,”a”]
Browser Support
| Chrome | 1 |
| Edge | yes |
| Firefox | 1 |
| Internet Explorer | 5.5 |
| Opera | yes |
| Safari | yes |
| Android webview | yes |
| Chrome for Android | yes |
| Edge mobile | yes |
| Firefox for Android | 4 |
| Opera Android | yes |