Changing the order of elements of an array by reverse() function

<script>
var array_no=[1,2,3,4] // Original array
array_no.reverse() // reversed 
document.write(array_no.join(",")) // display elements
</script>
Output is here
4,3,2,1
JavaScript arrays We can reverse the order of the element by using the reverse method. Here the first element will became last method and last element became first and other elements will also change their positions.

We have created an array of scripts and then displayed the elements. Then we have applied the command scripts.reverse(); to our scripts array and displayed again all the elements to see the result of reverse() function.

This method changes the original array.

join() takes a delimiter and create a string by adding all elements of the array
Example 2 , Here strings are used as elements of an array.
<script type="text/javascript">
var scripts = new Array();
 scripts[0] = "PHP";
 scripts[1] = "ASP";
 scripts[2] = "JavaScript";
 scripts[3] = "HTML";
 document.write(scripts.join(" , "));
 scripts.reverse();
 document.write("<br>- after reverse---<br>");
document.write(scripts.join(" , "));
 </script>
Output is here
PHP , ASP , JavaScript , HTML
- after reverse---
HTML , JavaScript , ASP , PHP
Arrange elements in alphabetical order
Array Reference How to display elements of an Array
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer