You can use the sort() method on an array in JavaScript to sort the elements in ascending order. The sort() method uses the built-in comparison function, so it can sort any type of elements, including numbers.

For example:

let numbers = [4, 2, 9, 1, 3];
numbers.sort();
console.log(numbers); // [1, 2, 3, 4, 9]

If you want to sort the array in descending order, you can pass a custom comparison function as an argument to the sort() method:

let numbers = [4, 2, 9, 1, 3];
numbers.sort(function(a, b) {
  return b - a;
});
console.log(numbers); // [9, 4, 3, 2, 1]

Alternatively, you can use the arrow function syntax:

let numbers = [4, 2, 9, 1, 3];
numbers.sort((a, b) => b - a);
console.log(numbers); // [9, 4, 3, 2, 1]

Also Read:

Categorized in: