One of my favourite pastimes is to find ways to highlight what a mess JavaScript is.

  • 9
    Bonus even though I know abusing the equalish operator isn't as exciting.
  • 3
    Reminds of JS Wat video.
    Make sure you document these on a public page.
  • 10
    Let me explain the things to you:

    You defined x, and then gave it the value of x, which was `undefined`.

    x++ is a macro for x= x + 1, undefined is not a number so it gets converted to NaN, and added 1.

    NaN is a "number" primitive value, just like Infinity, which you can't do most operations on.


    0 == !"0"
    Alright, intrepreter first converts the "0" to a boolean, the string is not empty, so it's true. Not'ted and `false`.

    0 is a falsy value, so you compare "false == false"

    0 == "0"

    left side is number, right side is string, let's make a number from "0", which makes 0.
    0 == 0 is true.
    0 === "0" wouldnt be true because you also compare the types and block implict conversions.
  • 3
    @anux Batman!
  • 4
    @melezorus34 oh, I know why they happen. That doesn't make the implicit behavior less of a meme.

    At no point would I want to do this without error or warning, but there's nothing stopping you from incrementing global or bitshifting an empty array into window.
  • 3
    @ltlian If it's a meme, tag it as such. Ranting on language features that lie as basis for the language itself is just being dumb.

    @melezorus34 Not entirely correct regarding the ++, but close enough I guess...
  • 4
    @ScriptCoded why is it dumb to rant about the fundamentals of a language?
    Because we cannot change it? Because it is how the language is? Thats exactly the reason to rant about it, imo.

    Plus, it’s funny. It’s funny, not because we don’t know why that happens, but because we do know it and the language designer decided that it would be a good idea. Or they couldn’t see the implications at that time. That makes it funny and valid to rant about.
  • 4
    e.g. "reeeeee junior javascript devs secure 2x my dotnet salary javascript bad"
  • 3
    This thread got weird.
  • 2
    Yep, JavaScript has the worst autocasting.
    It is like someone had rolled dice to determine what would cast to what in which situation.

    If you have to do code review for JavaScript, you should consider banning any use of automatic casting.
  • 4
    @ltlian @melezorus34

    I think the issue is not that these examples can't be explained, but that they can be hard to spot and interpret for a developer.

    The bad parts of Javascript is not caused by inconsistency -- It's actually a fairly consistent language -- It's about comprehensibility.

    Duck typing is dangerous. Sure, it walks and quacks like a duck, BUT I FUCKING MEANT FOR IT TO BE A GOOSE DAMMIT.

    The language might have strict, consistent rules for type conversions, but those rules must be uncomplicated and unambiguous for the developer.

    In my opinion, a program should break on a less straightforward conversion/inference.

    Turning an int into a float... meh, it's all just numbers. But turning a string into an int, or an array into a bool -- That should always be done explicitly.
  • 2
    @Lensflare Yeah I worded that badly. What I'm trying to say is that ranting on language features and underlining that it's "a mess" and essentially implying that it doesn't make sense, when in fact it's quite logical while also stating that you know the language and why it happens - that's not very bright.
  • 0
    @ltlian man, at this mind twister

    0 == '0'
    0 == !'0'

    I lost it 😂 It must be some sort of meme! Damn...
  • 0
    I see nothing wrong.
  • 3
    @ltlian Everything about JavaScript gets weird. It has a reason but it's still weird.
  • 1
    @ScriptCoded if you understand the language and know why it happens you are in the best position possible to rent about it.
  • 1
    @melezorus34 Big ups! Rarely have I see NaN quirks explained so quickly
  • 0

    Try using await sometime and look at their weird ass rules from doing it from the main calling scope if you want to see a mess !
Add Comment