Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

how to sort array numbers in ascending order in javascript

// how to sort array numbers in javascript without mutating original array.
const numbers = [100, 25, 1, 5];
const sorted = numbers.slice().sort((a, b) =>  a - b); // returns a new sorted array

console.log(numbers); // [100, 25, 1, 5]
console.log(sorted); // [1, 5, 25, 100]
 
PREVIOUS NEXT
Tagged: #sort #array #numbers #ascending #order #javascript
ADD COMMENT
Topic
Name
1+8 =