Page 246 - Web_Application_v2.0_C12_Fb
P. 246
9. The reverse() Method: The reverse() method reverses the order of the elements in the array. It
modifies the original array.
Example 54: To demonstrate the use of reverse() method
<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE>reverse Array Method</TITLE>
</HEAD>
<BODY>
<H2>The reverse() Method</H2>
<P>Original Array: </P>
<P id="demo1"></P>
<P>Array after using reverse method: </P>
<P id="demo2"></P>
<SCRIPT>
const Fruits = ['Apple', 'Mango', 'Banana', 'Papaya'];
document.getElementById("demo1").innerHTML = Fruits;
document.getElementById("demo2").innerHTML = Fruits.reverse();
</SCRIPT>
</BODY>
</HTML>
Output:
10. The sort() Method: The sort() method sorts the elements of an array in ascending order by default.
It sorts the array in place and returns the sorted array. This works well for strings ("Apple" comes before
"Banana"). However, if numbers are sorted as strings, "35" is bigger than "100", because "3" is bigger than
"1". Because of this, the sort() method will produce an incorrect result when sorting numbers.
Example 55: To demonstrate the use of sort() method
<!DOCTYPE HTML>
<HTML>
<HEAD>
244 Touchpad Web Applications (Ver. 2.0)-XII

