Array Sort in JavaScript

Level 253 keystrokes

sort() sorts array elements in place. By default, it sorts as strings.

JavaScript code

// Create an array with numbers
1let nums = [3, 1, 4, 1, 5];
// sort() sorts the array in place (modifies original)
// Default sort converts to strings and sorts alphabetically
// All numbers here have one digit, so the result is [1, 1, 3, 4, 5]
2console.log(nums.sort());

Output

[ 1, 1, 3, 4, 5 ]
Practice typing this code

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.

  • ,× 4
  • ;× 2
  • .× 2
  • (× 2
  • )× 2
  • [× 1
  • ]× 1
  • =× 1

More JavaScript examples at level 2

← JavaScript typing practice