June 6, 2023
Program to use JavaScript Array sort() method
let a = [1,3,5,2,4];
let b = a.sort();
console.log(b);
Output
[ 1, 2, 3, 4, 5 ]
Steps Description
- Declare two variable
a
andb
- Initialize two variable with value of array
a = [1,3,5,2,4]
andb = a.sort()
- Array
sort()
method sorts the array from descending to ascending order. - The value of
b
variable is displayed with the help of console.log inbuilt function
Program to use JavaScript Array sort() method
let c = [ 'Cat', 'Dog', 'Tiger', 'Lion', 'Elephant' ];
let d = c.sort();
console.log(d);
Output
[ 'Cat', 'Dog', 'Elephant', 'Lion', 'Tiger' ]
Steps Description
- Declare two variable
c
andd
- Initialize two variable with value of array
c = [ 'Cat', 'Dog', 'Tiger', 'Lion', 'Elephant' ]
andd = c.sort()
- Array
sort()
method sorts the array from descending to ascending order. - The value of
d
variable is displayed with the help of console.log inbuilt function
by : Suhel Akhtar
Quick Summary:
JavaScript Array sort() method