JavaScript

May 24, 2023

Ascending And Descending Order In JavaScript

Ascending And Descending Order


let arr = [3, 5, 4, 9, 2, 1];

arr.sort((a, b) => a - b); //  for ascending order
console.log(arr);

arr.sort((a, b) => b - a); //  for discending order
console.log(arr);

Output


[ 1, 2, 3, 4, 5, 9 ]
[ 9, 5, 4, 3, 2, 1 ]

by : Nehal Akhtar

Quick Summary:

Ascending And Descending Order SImple code easy Learn In JavaScript