Sorting an Array

The sort() sorts the elements of an array.

The sort() overwrites the original array.

The sort() sorts the elements as strings in alphabetical and ascending order.

Syntax

array.sort()
<!DOCTYPE html>
<html>
<head>
  <title>JavaScript</title>
  <script>
    var a = ["Umar","Zaid","Rehman","Hamza"];
       
    document.write(a + "<br><br>");
    a.sort();
    document.write(a);
  </script>
</head>
<body>
</body>
</html>

Reversing an Array

The reverse() method reverses the order of the elements in an array.

The reverse() method overwrites the original array.

Syntax

array.reverse()

Return Value

The array after it has been reversed.
<!DOCTYPE html>
<html>
<head>
  <title>JavaScript</title>
  <script>
    var b = ["Uamr", "Zaid", "Rehman", "Hamza"];

    document.write(b + "<br><br>");
    b.reverse();
    document.write(b);
  </script>
</head>
<body>
</body>
</html>