3

Almost two decades of working with JavaScript, and I just got caught out by the most stupid of things..

Did you know that `["ping"] == "ping"` is `true`, whereas `["ping"] === "ping"` is `false`...? 🤔 I did, once upon a time, long ago. Apparently I forgot about it though.

Seriously, fuck you JS.. with all of the internet's 10 foot rusty barge poles.

Comments
  • 1
    Explanation how the first one evaluates to true because of ==?
  • 4
    Excuse me WHAT
    I thought I knew all the silly games JS equality can pull. Guess there really is no alternative to banning ==
  • 3
    @cb219 this would be because the non-strict equality check tries to convert the items being compared to the same type first.. so, the array gets stringified, and `["ping"].toString()` returns `"ping"`. If I remember correctly, the "Array.prototype.toString" method literally just concatenates the values with a comma.

    @lorentz indeed! I've been using only the strict comparators for so long, maybe that's why this one caught me out... But alas, when you're working with other people's code, well... Yeah.
  • 6
    Did not expect that.
    Just tested and “hi” == [[“hi”]] also return true.

    Js is like my girlfriend…crazy
  • 1
    As much as I dislike JS, it's PERFECT for webdev. You expect a few things to fail, JS will try to fill the gaps and keep the show going
    I'd prefer an off-looking website coz JS fuckery over a 'website runtime error' lol
  • 0
    @ostream that, unfortunately, was already in the project.. but this particular project has many, many, many other issues that are much bigger.

    I guess in short, I'm trying to "rescue" a mission critical business webapp, working like mad to fix things without rewriting, because "we don't have time to rewrite from scratch"..... 🤦

    The real irony here, is that this project is in such a scary bad state, that it would actually take less time to just rewrite it. But alas, I've been telling my bosses that for the last 5 years, and I get the same reply each time.
  • 0
    I'm not sure in which use case I will ever need to use it :-/
  • 0
    @IdontHaveAName
    ['Hello', ' World'] == 'Hello, World'
  • 1
    Which is why a lot of people keep telling JS devs that JS sucks.

    Not that the JS devs listen... :)

    Have some 🥞.
  • 0
    @IntrusionCM oh indeed, and I fully agree.. JS sucks donkey dick with great enthusiasm. But it's still a necessary evil.

    FWIW, TS does a lot to help fix the "developer experience", but I'm having way too much fun getting to grips with Dart these days to care about that anymore. 🖖
  • 2
    @azuredivay No, it‘s not perfect for webdev.
    Optimally, this kind of mistake should have been caught by the compiler/transpiler. Not at runtime.
    The worst kind of bug is this kind of shit that fails silently and looks like nothing wrong happened. When in reality you have a bug in the system.
  • 2
    @Lensflare as a _runtime_, JS really is an amazing "thing".. the sheer number of platforms that can run it makes it almost endlessly portable. But with great power, and all that.... Let's just say it's real easy to write bad code in JS.. and to still actually have it "work".

    For what it's worth, I do not advocate the writing of JavaScript per se, I believe it's better to write in TypeScript, which obviously doesn't let shit like this happen.

    And having said that, JS can run anywhere, sure.. but that doesn't mean it should. 🤔
Add Comment