JavaScript Code


How to sort array of numbers in ascending order in JavaScript?


Sort array of numbers in ascending order in JavaScript

In this tutorial, you are given an array of numbers. You should 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