re: 10 JavaScript array methods you should know VIEW POST

VIEW PARENT COMMENT VIEW FULL DISCUSSION

Thanks for that buddy, i didn't know that .sort() without passing callback it sort element in ascending order

const alpha = ['e', 'a', 'c', 'u', 'y'];
console.log(alpha.sort()); // output ["a", "c", "e", "u", "y"]

.. now here is problem

const nums = [3, 4, 21, 100];
console.log(nums.sort()); // output [100, 21, 3, 4]

// In opinion, should always do this
console.log(nums.sort((a, b) => a > b ? 1:-1)); // output [3, 4, 21, 100]
code of conduct - report abuse