• Kevin@lemmy.world
    link
    fedilink
    English
    arrow-up
    17
    ·
    1 year ago

    From MDN:

    If compareFn is not supplied, all non-undefined array elements are sorted by converting them to strings and comparing strings in UTF-16 code units order.

    So, if you’re trying to sort numbers, [6, -2, 2, -7].sort((a, b) => a - b)

    • skates@sh.itjust.works
      link
      fedilink
      English
      arrow-up
      4
      ·
      1 year ago

      That’s a good point lol, I haven’t done a js interview yet but it’d probably be kinda rough trying to remember random shit like this.

    • EatMyDick@lemmy.world
      link
      fedilink
      English
      arrow-up
      1
      arrow-down
      2
      ·
      edit-2
      1 year ago

      Meh, it’s a day 2 thing to understand how shitty JS deals with numbers. Good devs will cover their asses by default.