How toSorted() method is different from sort() in JavaScript Last Updated : 05 Dec, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report In JavaScript, sort() and toSorted() methods do the same job as sorting but toSorted() does not change the original array. sort() method changes the original array. Below we have explained both the methods. Table of Content Sort() MethodtoSorted() MethodSort() Method:The sort() method is a built-in method for arrays in JavaScript that directly modifies the array in place.By default, it sorts the elements as strings, so for numeric sorting, a comparison function needs to be provided.Syntax:arr.sort()Example: In this example, We have used sort() method to sort an array. Here, we can see that sort() method change the original array. JavaScript let arr = [3,4,1,2,8,2]; let arr2 = arr.sort(); console.log("arr = ",arr); console.log("arr2 = ",arr2); Output:arr = [ 1, 2, 2, 3, 4, 8 ]arr2 = [ 1, 2, 2, 3, 4, 8 ]toSorted() Method:The toSorted() method of array instances is the copying version of the sort() method. It returns a new array with the elements sorted in ascending order and original array won't get affected. Syntax:let arr2 = arr.toSorted()Example: In this example, We have used toSorted() method. Here we can see that toSorted() method does not change the original array. JavaScript let arr = [3,4,1,2,8,2]; let arr2 = arr.toSorted(); console.log("arr ="arr); console.log("arr2 = "arr2); Output:arr = [3, 4, 1, 2, 8, 2]arr2 = [1, 2, 2, 3, 4, 8] Comment More infoAdvertise with us Next Article How toSorted() method is different from sort() in JavaScript U ukasp Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How toReversed() method is different from reverse() method in JavaScript In JavaScript, reverse() and toReversed() methods do the same job as reversing but toReversed() does not change the original array. The reverse() method changes the original array. Below we have explained both methods. Table of Content Reverse() MethodtoReversed() methodReverse() MethodThe reverse() 2 min read JavaScript Array toSorted() Method The JS arr.toSorted() method rearranges the array elements alphabetically and returns a new sorted array.Sorting Array of StringsJavaScriptconst arr = ["JS", "HTML", "CSS"]; const arr1 = arr.toSorted(); console.log(arr); console.log(arr1);Output[ 'JS', 'HTML', 'CSS' ][ 'CSS', 'HTML', 'JS' ]Array.toS 3 min read How to Sort a Map in JavaScript? Sorting a Map in JavaScript involves ordering its key-value pairs based on the keys or values. Since Maps maintain the insertion order, you can't directly sort them like arrays. Instead, you'll need to convert the Map into an array, sort it, and then convert it back into a Map.Below are the approach 3 min read How to sort a collection in JavaScript ? A javascript collection is much like a container. It's just an item that combines several elements into a single unit. Aggregate information is stored, accessed, modified, and communicated via collections. With the help of constructors, we create collections in javascript. In earlier versions of jav 3 min read How are elements ordered in a Map in JavaScript ? In JavaScript, a new object called Map was introduced in the ES6 version. A map is a collection of elements where each element is stored in a key, value pair. Map objects can store both objects as well as primitive data types. The elements of a map are iterable. Elements are always iterated in the i 2 min read Like