JavaScript is weird

  • I agree JS has a high number of quirks, but like 22 out of these 25 are "implicit type conversion is surprising".

    It's surprising in almost every language that does it to any degree. JS's is only slightly stranger than, say, PHP. About the only exception is "empty values are falsy", and even that surprises people pretty frequently, and can be used to construct highly confusing snippets of behavior.

  • No, JS is not weird, your code is weird.

    true + ("true" - 0) ?

    "" && -0 ?

    Like, can you tell me a language where it does make sense to "apply AND to an empty string and a (negative?) zero"?

    If you write code like this, you have much bigger issues than whatever you choose to write your code in.

  • > 0.2 + 0.1 === 0.3

    This isn't just javascript though. It's harder to find a language for which this would be true than the other way around.

  • > This website is literally about JavaScript. I mean what did you expect, a .NET application? This website is 99.9% poorly optimized and highly questionable JS. And yet, you have JS turned off.

    I kinda expected text and code examples, on a blog. I forgot to check that this is a dedicated website about the weirdness of javascript.

  • My favorite thing about js that I recently learned is this:

    var arr = [];

    arr[2] = 2;

    // now arr is [empty, empty, 2]

    arr[-4] = -4;

    // now arr is [empty, empty, 2, '-4': -4]

    arr.length; // is 3

    arr["foo"] = "bar";

    // [empty, empty, 2, '-4': -4, foo: 'bar']

  • Who would ever code like this? Those kind of quizzes can be made for any language

    So tired of those smug "hurr durr language X hehe" posters

  •     parseInt(0.00005)
    
        // 0
        parseInt(0.000005)
    
        // 0
        parseInt(0.0000005)
    
        // 5
        parseInt(0.00000005)
    
        // 5
    
    Huh??!

  • Slightly challenging one. Find the potential bug.

       const arr = [1, 2, 3, 4, 5]
    
       for (let i = arr.length - 1; 0 < i; i--) {
          const j = Math.floor(Math.random() * (i + 1))
          [arr[i], arr[j]] = [arr[j], arr[i]]
       }

  • My favorite weird JS thing:

    ['10', '10', '10', '10', '10'].map(parseInt)

  • My favorite which wasn’t included is

    NaN === NaN -> false

    In fact that’s exactly how isNaN(x) is implemented under the hood. It compares the argument to itself.

  • Someone should write a book about this [0] and a tool to automate checking your JavaScript code [1].

    [0]: https://www.oreilly.com/library/view/javascript-the-good/978...

    [1]: https://www.jslint.com/

    I'm working on a book called "How to not get your knickers in a twist because you neglected to learn from people who came before you."

  • this is the reason I pick a language OTHER than js for the backend. Why oh why did the whole world decide nodejs sever side js was a good direction? For the frontend I get it. Unless you are going to mess with WASM files you need js.

  • no no no, python is weird

  • But no mention of "dog".sup()?

  • Very much why I write my front ends in Elm. It might be a PIA, but it shields me from quirks like this.

  • Get a linter