JS Coding Questions Logo
JS Coding Questions
#243💼 Interview💻 Code

How do you sort elements in an array

Advertisement

728x90

The sort() method is used to sort the elements of an array in place and returns the sorted array. The default sort order is ascending, based on the string Unicode order. The example usage would be as below,

javascript
1var months = ["Aug", "Sep", "Jan", "June"];
2
3months.sort();
4
5console.log(months); //  ["Aug", "Jan", "June", "Sep"]

Beware: sort() is changing the original array.

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 72

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
243of476