45

When you think you know enough Javascript to get a job, then this comes along Can (a ==1 && a== 2 && a==3)could evaluate to true, and the answer is freaking "yes" πŸ˜’πŸ˜’.

Comments
  • 10
    it's a feature.
  • 21
    Javascript is the proof that there are parallel universes.
  • 9
    Can someone explain how?
  • 3
    One more reason to dislike JS. But seriously, how? Does it have something to do with the spaces inbetween?
  • 6
    @CWins Just use === kek.
  • 6
    it works in every class based language where you can modify the comparison method
  • 6
    @virus Holy shit, that's nice. Especially the second answer(below the accepted one) is pretty crazy too:

    var aοΎ  = 1;
    var a = 2;
    var οΎ a = 3;
    if(aοΎ ==1 && a== 2 &&οΎ a==3) {
    console.log("Why hello there!")
    }

    (yes, this works...)
  • 1
    @deadlyRants that's bad if it doesn't overwrite...
  • 4
    @RAZERZ The trick here is that these are 3 different variables with invisible korean symbols or something, so JS interprets them differently but humans can't see that.

    This would work in other languages too(like the greek question mark stuff) but nobody would really use that(at least that's my hope).
  • 1
    @RAZERZ it does, I think there some special chars you can't see but take as start and end variable name in JS.
  • 0
    i don't get it.
    which means it's a proper valid and correct javascript.
  • 1
    I'm trying to force myself to learn js again and again but my brain resisted.. Things just doesn't work as I expected
  • 0
    @Zerocchi this is JS.
    expecting it to make sense is the first rookie mistake, along with believing it has any actual syntax and not just style conventions
  • 1
    @Zerocchi your brain resisting means you are still sane. run while you can
  • 0
    @Midnigh-shcode yeah I just don't know how to face it solely because of that reasons, knowing every other languages make much more sense than js. I don't have a choice either, until WebAssembly become de facto standard. Probably transpilers is the only choice.
  • 1
  • 0
    The answers on SO don't really show JS's flaws IMO, most of them are pure trickery like the one using visually undistinguishable korean characters for variable names.

    When I first heard that question, I was really excited to learn another JS quirk, I expected the answer to be messing with JS's coercion. Needless to say I'm disappointed, except for that answer which involves toString/valueOf.
  • 1
    just because js allows you to make weak comparisons doesn't mean you should use it. it could be used as an exception, a "hackish" solution, but it's still a bad practice. every single linter for js code marks this as an error by default. just use strict comparison "===" and you won't have a problem.
  • 2
    This is why you always want to use === instead of == in JS, no exceptions. It's not true that JS makes no sense or has no syntax. For historic reasons, it obviously has more quirks and weird behaviors than most other languages. But I'd argue that if you don't learn stuff like this during your first steps in JS, you should be looking for a different teacher or learning resource instead of bashing the language.

    Then again, it's pretty much impossible to know which of a language's basics you *should* be learning early, unless you already know these basics. Thus making it hard to judge the quality of your teacher/resource when starting out.

    The thing with the Korean symbols implicates a programmer who's actively looking to create code that seems to be behaving in a weird way, even though it actually isn't. It doesn't apply to real world situations and I'm sure similar stuff is possible in other languages as well.
  • 0
    This question is testing your knowledge of type coercion and comparison operators (i.e. == v.s. ===) which is something every JS dev should know (junior included).
Add Comment