About
In this code snippet, we’ll learn how to sort and reverse arrays in Javascript.
Let’s see the code example below.
Code:
let numbers = [ 3, 8, 7, 1 ]; let characters = [ "c", "s", "a", "n" ]; let strings = [ "car", "truck", "bike" ]; //Sort//////////////////////////////////////////////////// numbers.sort(); //Will sort array numerically. console.log(numbers); characters.sort(); //Will sort array alphabetically. strings.sort(); //Will sort array alphabetically. console.log(characters); console.log(strings); ///////////////////////////////////////////////////////// //Reverse//////////////////////////////////////////////// numbers.reverse(); //Will reverse elements in the array. console.log(numbers); /////////////////////////////////////////////////////////