Which built-in method reverses the order of the elements of an array?
Which built-in method reverses the order of the elements of an array?
Collapse
X
-
Tags: None
-
You can use revese method of javascript
example : array_name.reve rse();
you can try below code:
Code:<!DOCTYPE html> <html> <body> <h3>Click the button to reverse the order of the elements in the array.</h3> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> var cricketers = ["virat", "Rohit", "hardik", "Rahul"]; document.getElementById("demo").innerHTML = cricketers; function myFunction() { cricketers.reverse(); document.getElementById("demo").innerHTML = cricketers; } </script> </body> </html>
Comment
-
"a.reverse( );" : Replace the array
"a.slice().reve rse();" : Display only without replace the array
a.reverse(); - mutates the array a
a.slice().rever se(); - mutates a new array that was returned by the slice method - so the original array a would stay unmodifiedComment
Comment