JavaScript Array sort() method17 Mar 2025 | 2 min read The JavaScript array sort() method is used to arrange the array elements in some order. By default, sort() method follows the ascending order. SyntaxThe sort() method is represented by the following syntax: ParametercompareFunction - It is optional. It represents a function that provides an alternative sort order. ReturnAn array of sorted elements JavaScript Array sort() method exampleHere, we will understand sort() method through various examples. Example 1Let's see a simple example to sort the array of string elements. Test it NowOutput: AngularJS,Bootstrap,JQuery,Node.js Example 2Let's see a simple example to sort the array of integer elements. Test it NowOutput: 1,2,4,5,8 Example 3Let's see an example to arrange the elements in ascending order using function. Test it NowOutput: 1,2,4,5,8 Example 4Let's see an example to arrange the elements in descending order using function. Test it NowOutput: 8,5,4,2,1 Example 5Let's see an example to extract minimum value from an array. Test it NowOutput: 1 Example 6Let's see an example to extract maximum value from an array. Test it NowOutput: 8 Next TopicJavaScript Array |
JavaScript Array method The JavaScript array method is used to invoke the specified function once for each array element. Syntax The method is represented by the following syntax: array.forEach(callback(currentvalue,index,arr),thisArg) Parameter callback - It represents the function that test the condition. currentvalue - The current element of array. index - It...
1 min read
JavaScript Array method The JavaScript array method checks whether the given array contains the specified element. It returns true if an array contains the element, otherwise false. Syntax The method is represented by the following syntax: array.includes(element,start) Parameter element - The value to be searched. start - It is...
1 min read
JavaScript Array method The JavaScript array method returns the index of first element of the given array that satisfies the provided function condition. It returns -1, if no element satisfies the condition. Syntax The method is represented by the following syntax: array.findIndex(callback(value,index,arr),thisArg) Parameter callback - It...
1 min read
JavaScript Array method The JavaScript array method returns the first element of the given array that satisfies the provided function condition. Syntax The method is represented by the following syntax: array.find(callback(value,index,arr),thisArg) Parameter callback - It represents the function that executes each element. value - The current element...
1 min read
JavaScript Array method The JavaScript array method adds one or more elements to the end of the given array. This method changes the length of the original array. Syntax The method is represented by the following syntax: array.push(element1,element2....elementn) Parameter element1,element2....elementn - The elements to be added. Return The original array...
1 min read
JavaScript Array Method The method is a combination of flat() and map() methods. This method initially maps each array element through a mapping function, then flatten up the array with depth value as 1. Syntax var newArr=arr.flatMap(function callback(currentValue[ , index[ , array]]) { return element } [ , thisArg]) Parameters callback-...
2 min read
JavaScript Array method The JavaScript array method is used to search the position of a particular element in a given array. This method is case-sensitive. The index position of first element in an array is always start with zero. If an element is not present...
1 min read
JavaScript Array method The JavaScript array method filter and extract the element of an array that satisfying the provided condition. It doesn't change the original array. Syntax The method is represented by the following syntax: array.filter(callback(currentvalue,index,arr),thisArg) Parameter callback - It represents the function that test the condition. currentvalue -...
1 min read
JavaScript Array method The JavaScript array method adds one or more elements in the beginning of the given array and returns the updated array. This method changes the length of the original array. Syntax The method is represented by the following syntax: array. unshift(element1,element2,....,elementn) Parameter element1,element2,....,elementn - The...
1 min read
JavaScript Array Method The method creates and returns a new array from different a number of arguments. It does not focus on the type and number of arguments. Similar to Array, it also provides a constructor that handles the integer arguments, but in a...
2 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India