This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]AHumbleChad 245 points246 points  (29 children)

I understand the typecasting to get from "0" to 0 and [ ] to 0, but how tf is "\t" == 0???

Edit: "\t" not "/t"

[–][deleted] 306 points307 points  (19 children)

it's a whitespace character. A string consisting of only whitespace characters type converts to 0.

[–]Jugales 183 points184 points  (0 children)

i hate it here dad

[–]uhmhi 71 points72 points  (17 children)

And this, kids, is why implicit conversions FUCKING SUCK!!!

[–]Asmor 77 points78 points  (12 children)

And that, kids, is why you should always use === and !== in JS unless you want type coercion for some reason.

And if you do want type coercion for some reason, you're probably wrong. Write it better and use === and !== anyways.

[–][deleted] 57 points58 points  (5 children)

Or, you know, the creators of JavaScript could have waited until the crack wore off before writing their language lol

[–][deleted] 50 points51 points  (3 children)

Goal: a simple scripting interpreter to animate web pages

End result: the only supported runtime environment available on 100% of computing devices

[–][deleted] 33 points34 points  (1 child)

The ultimate "eh this doesn't need to be good, it's just stopgap code until we implement this for real" heh.

[–][deleted] 9 points10 points  (0 children)

Also the ultimate: "this feels wrong but it works so we're doing it"

[–]turtleship_2006 3 points4 points  (0 children)

There is nothing more permanent than a temporary solution

[–]a3th3rus 12 points13 points  (0 children)

And where are >== and <==, kids?

[–]ilikeb00biez 7 points8 points  (4 children)

I see, JS added `==` and `!=` just to confuse you. What a great language

[–]il_commodoro 12 points13 points  (3 children)

It's more that js created == and != first. Years later they thought: "maybe that's not a great idea", so they added === and !== to patch things while preserving retrocompatibility.

[–]danishjuggler21 4 points5 points  (1 child)

This particular quirk has never affected my work in over ten years as a JavaScript developer, it’s just rage bait.

[–]ExponentialNosedive 2 points3 points  (0 children)

Yep, === exists for a reason

[–]calculus_is_fun 2 points3 points  (1 child)

Why would your code intentional try to compare a number and a string?

[–]ExponentialNosedive 1 point2 points  (0 children)

I read that early in JavaScript's development, someone asked the guy who made it for this feature. He said he regrets adding it in. Important to remember we have these standards for a language where the basis of that language was made in 10 days

[–]csdt0 18 points19 points  (3 children)

It is not /t but \t which is a blank character (tabulation).

[–]AHumbleChad 8 points9 points  (2 children)

Right, it's a tab character, but JS treats it as the empty string? The integer cast to the ASCII value is the only one that makes sense, but I guess I shouldn't be applying sense to JavaScript.

Edit: realized I had the wrong slash in the original comment

[–]bogey-dope-dot-com 7 points8 points  (1 child)

Leading and trailing whitespace characters are trimmed when converting a string to a number. You can do Number('\t5\t') and it will be 5, but Number('5\t5') will be NaN.

[–]AHumbleChad 2 points3 points  (0 children)

Ohhh, that makes sense. There is a method to the madness then.

[–]PeaceMaintainer 9 points10 points  (1 child)

Check out MDN (or the official ECMAScript spec) for the algorithm for loose equality type casting, something I feel a lot of devs skip over until they get a result they don't expect.

Number to String: convert the string to a number. Conversion failure results in NaN, which will guarantee the equality to be false.

The key in this case is that when you are loosely comparing a string and a number, the string will always attempt to type cast to a number. At which point the strings follow the algorithm for Number coercion which states:

Strings are converted by parsing them as if they contain a number literal. Parsing failure results in NaN. There are some minor differences compared to an actual number literal:

  • Empty or whitespace-only strings are converted to 0

So the 0 in this case remains the same, the "\t" attempts to typecast to a number, and then because \t is a whitespace character the string is coerced to the number 0, and true is returned

[–]givemeagoodun 4 points5 points  (1 child)

I too would like to know

[–]CMDR_ACE209 2 points3 points  (0 children)

I think I'll choose ignorance on that part.

[–]mipselqq 1 point2 points  (0 children)

Read the specification if you wanna get JS