The sort()
method is used to sort the items that are present inside the array.
array.sort()
The sort()
method attaches to the right side of an array with the .
operator. The sort method sorts the items present in the array in ascending order.
const alphabets = ['k','a','l','i','o','p'];
console.log(alphabets.sort()); // Prints: [ 'a', 'i', 'k', 'l', 'o', 'p' ]
const numbers = [7,1,9,4,6,7];
console.log(numbers.sort()); // Prints: [ 1, 4, 6, 7, 7, 9 ]
The sort()
method sorts both numbers and alphabets, as we can see in the example above.