Array toSorted in JavaScript
Level 3116 keystrokes
toSorted() returns a new sorted array without mutating the original array.
JavaScript code
// Create an array of numbers1const nums = [3, 1, 4, 1, 5];// toSorted() creates a new sorted array (ascending order)// The compare function (a, b) => a - b sorts numerically2const sorted = nums.toSorted((a, b) => a - b);// Output the sorted array: [1, 1, 3, 4, 5]3console.log(sorted);// Original array remains unchanged: [3, 1, 4, 1, 5]4console.log(nums);Output
[ 1, 1, 3, 4, 5 ] [ 3, 1, 4, 1, 5 ]
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this JavaScript code and how often they appear.
,× 5;× 4(× 4)× 4.× 3=× 3-× 1[× 1]× 1>× 1