Horje
What is the use of the Array.sort() method in JavaScript ?

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); // Output: [1, 2, 4, 6, 8]

Output

[ 1, 2, 4, 6, 8 ]



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
What is the use of && operator in JavaScript ? What is the use of && operator in JavaScript ?
What is the use of the += operator in JavaScript ? What is the use of the += operator in JavaScript ?
What are Comparison Operators in JavaScript ? What are Comparison Operators in JavaScript ?
Difference between Function Declarations & Function Expressions in JavaScript Difference between Function Declarations & Function Expressions in JavaScript
What is the use of the return Keyword in JavaScript Functions ? What is the use of the return Keyword in JavaScript Functions ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13