Aryan PrajapatKnowledge Contributor
What will be the output of the below code? var myarray =new Array (1,4,3,6,10,0,22) document.write(myarray.sort()) myarray.sort(function(a, b) { return b – a; }); document.write(myarray);
What will be the output of the below code? var myarray =new Array (1,4,3,6,10,0,22) document.write(myarray.sort()) myarray.sort(function(a, b) { return b – a; }); document.write(myarray);
Ans: [0,1,10,22,3,4,6] and [22,10,6,4,3,1,0]
The sort() method sorts the elements of an array. The sort order can be either alphabetic or numeric, and either ascending (up) or descending (down). By default, the sort() method sorts the values as strings in alphabetical and ascending order.