JavaScript Code

How to sort array of numbers in ascending order?


Problem Statement

given an array of numbers, write a JavaScript program to sort the array in ascending order

Solution

to sort an array of numbers in ascending order, use array sort() method. sort() method sorts the elements in ascending order, by default

Program

numbers is the given array of numbers. we use sort() to sort numbers in ascending order

let numbers = [5, 1, 7, 3, 2, 0];
numbers.sort();
console.log(numbers);


copyright @2022