// Original array and sorting
let arr = [5, 2, 8, 1, 4];
console.log('Original Array:', arr);
// Sorting the array using the sort method
arr.sort();
console.log('Sorted Array:', arr);
code
Sorting an Array in JavaScript
This JavaScript code demonstrates how to use the sort method to sort an array. The sort method sorts the elements of an array in place and converts them to strings for comparison. Keep in mind that the default sorting order is lexicographic (dictionary order), which may not produce the expected numeric sort in all cases. For a numeric sort, you can provide a compare function to the sort method.
Array
Sorting
Sort Method