473,396 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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?
May 22 '19 #1
4 2858
gits
5,390 Expert Mod 4TB
there is an Array method called: reverse
May 22 '19 #2
AjayGohil
83 64KB
You can use revese method of javascript

example : array_name.reverse();

you can try below code:

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <h3>Click the button to reverse the order of the elements in the array.</h3>
  6.  
  7. <button onclick="myFunction()">Try it</button>
  8.  
  9. <p id="demo"></p>
  10. <script>
  11. var cricketers = ["virat", "Rohit", "hardik", "Rahul"];
  12. document.getElementById("demo").innerHTML = cricketers;
  13.  
  14. function myFunction() {
  15.     cricketers.reverse();
  16.   document.getElementById("demo").innerHTML = cricketers;
  17. }
  18. </script>
  19.  
  20. </body>
  21. </html>
  22.  
  23.  
Feb 20 '20 #3
SioSio
272 256MB
> var a = [1, 2, 3]
undefined
> a
[1, 2, 3]
> a.reverse();
[3, 2, 1]
> a
[3, 2, 1]
> a.slice().reverse();
[1, 2, 3]
> a
[3, 2, 1]

"a.reverse();" : Replace the array
"a.slice().reverse();" : Display only without replace the array
Feb 20 '20 #4
gits
5,390 Expert Mod 4TB
"a.reverse();" : Replace the array
"a.slice().reverse();" : Display only without replace the array
more accurate would be:

a.reverse(); - mutates the array a
a.slice().reverse(); - mutates a new array that was returned by the slice method - so the original array a would stay unmodified
Feb 20 '20 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Csaba2000 | last post by:
I've been thinking the replies to my earlier post. I can attach a method to a DOM element as below. But instead of assigning it to each element individually, I would rather have it as a method...
2
by: _andrea.l | last post by:
I'd like to roder an array: $array $array $array I'd like to order $array by name or by numbre of by a function($array,$array) that return 1 or -1 if id1 is less tha id2 or viceversa... How...
1
by: Tomas | last post by:
Is there any sequence diagram on the web that clearly shows in which order all Page methods (load, render and so on) are being called compared to the order the page's contained control methods are...
5
by: mike | last post by:
hi, how can i use the object as the parameter of the method, in the body of a class in which the method is defined ? example: assume i've got a class imaginary_number, and one of the methods...
8
by: digitalorganics | last post by:
What are the reason one would get this error: TypeError: Cannot create a consistent method resolution order (MRO) for bases object ?? I can provide the code if needed....
1
by: Vivek | last post by:
Hi All, I need some help with showing option elements of a multiple select list in a particular order. Is it possible to order the elements according to the value of the option or the text of...
2
by: Steve Richter | last post by:
Is it possible to write a generic method which returns true or false, depending on if the array contains a value? Is there already a framework method that provides this functionality? the...
2
by: pplers | last post by:
In an array, what's "bigger" ?? "aaaa1111" or "aaaa" or "1111" or "1111aaaa" OR 'aaaa1111' or 'aaaa' or '1111' or '1111aaaa' Because i want to order an array, for example $arr = array('4wf',...
2
by: Klaudiusz Bryja | last post by:
Hi, I need to get event name which call method. How do I do this? For example - I have button with subscribed Click event and e.g. GotFocus. For both this events I have the same method which...
2
by: anyaservices | last post by:
Hi I have multiple files (between 50-100) that basically contain simple text. Each file ranges in size from few MB to 50MB. I have to concatenate all these files to produce one single file that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.