Javascript - predict operation result
Rodion Gorkovenko

Rodion Gorkovenko @rodiongork

About: Developer, Dreamer, School-Teacher, author of CodeAbbey and few other silly projects :)

Location:
Saint-Petersburg, Russia
Joined:
Jan 16, 2020

Javascript - predict operation result

Publish Date: Jan 16 '20
3 4

Sorry for this may be well-known for some people, especially professional JS devs. But JS is so widely used in projects that I expect some others (like me) may find some results surprising.

Predict (try) result of operations (including type!) of few following expressions - and then check yourself (e.g. in browser console).

  1. "5" + 3
  2. "5" - 3
  3. [] + 2
  4. {} + 2
  5. [] - 2
  6. {} - 2
  7. [] + 2 == 2 + []
  8. {} + 2 == 2 + {}
  9. {} + []
  10. [] + {}

Post in the comment, well... No answers (everyone can check)... Post how many you've guessed. And which is the most confusing in your opinion.

Few are obvious so hopefully your result would be non-zero. No sense to cheat, we are not in interview or exam - just have fun! :)

Comments 4 total

  • Amin
    AminJan 29, 2020

    A little late to the party!

    Surprisingly, I couldn't successfully guess the third one and the last three. JavaScript is really a weird language haha!

    This is astonishing knowing that a language that can interpret these instructions is ruling the Web...

    • Rodion Gorkovenko
      Rodion GorkovenkoFeb 5, 2020

      I couldn't successfully guess the third one and the last three

      Oh, that's cool! I couldn't guess almost all of them. However I'm more backend developer and use JS occasionally. Nevertheless it is funny that people blame PHP for being illogical when there is JS :)

      • Amin
        AminFeb 5, 2020

        That's why I am loving developing applications using Elm. It is a language that has all the necessary tools to build modern websites as well as web applications or even progressive web apps.

        It is really shrunk down to the core utilities and has types which means it is safer to use than JavaScript.

        I wish types were more of a thing in PHP but its already really good at the moment, especially with the 7.4 release of PHP.

        • Rodion Gorkovenko
          Rodion GorkovenkoFeb 5, 2020

          using Elm

          thanks for this hint - as I'm not neck-deep in front-end, I often miss new trends, so I'll look into this :)

          However I know that often languages are chosen not because of their specific properties, but because of "social" consideration - including how many people use it, how many support and how hard it could be to hire programmers for it etc :)

Add comment