What is the use of the Array.sort() method in JavaScript ?
Last Updated :
12 Feb, 2024
The Array.sort()
method is used to sort the elements of an array in place and returns the sorted array.
Example: Here, the sort()
method is called on an array of numbers. By default, sort()
sorts elements as strings, so it sorts the numbers based on their Unicode code points. As a result, the numbers are sorted in ascending order. The sorted array is then logged to the console.
Javascript
let numbers = [4, 2, 8, 1, 6];
numbers.sort();
console.log(numbers);
|
Share your thoughts in the comments
Please Login to comment...