• bobo@lemmy.ml
    link
    fedilink
    arrow-up
    2
    ·
    18 hours ago

    Ok, except I did predict it. It turns them both into strings and gives you “12”. I checked it.

    I would assume [1] + [2] would give you either 0 or 2, but maybe “12”.

    I can totally predict the future like that. Tomorrow it might rain, it might not, or it might snow. Want me to predict your future for a small donation? Here’s a small teaser: tomorrow you might or might not eat breakfast.

    My mystical powers lay the creation bare before me, all is predictable!

    • hperrin@lemmy.ca
      link
      fedilink
      English
      arrow-up
      1
      ·
      9 hours ago

      Here’s my thought process: plus is for numbers and strings, so it’s gonna convert the arrays to either numbers or strings. If it converts them to 0, the answer is 0, if it converts them to 1, the answer is 2, if it converts them to strings, the answer is “12”.

      You know what I didn’t say? [1,2]. Because plus is not for array concatenation. The question is meant to make you think you’ll get [1,2], because why else would you use plus on arrays?

      In a language that uses plus for concatenation, you’ll see that kind of code all over, and know what you’ll get. But you never see that in JavaScript, because that’s not how we concatenate arrays.