719
you are viewing a single comment's thread
view the rest of the comments
[-] sbv@sh.itjust.works 29 points 6 months ago
[-] Limitless_screaming@kbin.social 18 points 6 months ago* (last edited 6 months ago)

that's not "t", it's "\t" which is just a tab. There's also "\n" for newline.

[-] Sonotsugipaa@lemmy.dbzer0.com 20 points 6 months ago

It still makes no sense though

[-] Limitless_screaming@kbin.social 22 points 6 months ago

If " " wasn't equal to 0, it wouldn't make sense, but since a string containing a space equals 0, you'd expect the same to apply to a string containing a tab or a newline. (or at least I'd expect that)

[-] FaceDeer@kbin.social 5 points 6 months ago

I admit I have never dabbled in javascript, despite being a proficient programmer. I now dread to ask... would any string that contains only whitespace == 0? " \t\n \t " for example?

[-] Limitless_screaming@kbin.social 6 points 6 months ago

Yes, it would. Just like a string of spaces " " == 0, but it isn't that bad; === is Javascript's version of == in other languages, and, thus, you should be using it if you don't want that wonkiness.

== is just for convenience, like when you want to make sure that the user didn't leave the form empty and the button shouldn't be greyed out, and other UI stuff. Without these kinds of features JS wouldn't be used in so many toolkits.

[-] atx_aquarian@lemmy.world 2 points 6 months ago

Ok, I always mistakenly assumed === was the identity operator in JS, too. TIL, thanks! As much as we like to poke fun at JS, every time I'm taught the rationale behind some aspect of it, I find it redeeming and even a little endearing.

[-] bitcrafter@programming.dev 5 points 6 months ago

The explanation given to you makes it sound like == was deliberately designed to be a more convenient version of ===, but what actually happened was that == used to be the only equality operator in JavaScript, which meant that if you didn't want it's auto-coercing behavior then you needed to go out of your way to add additional type checks yourself. Because this was obviously a tremendously inconvenient state of affairs, the === operator was introduced later so that you could test for equality without having to worry about JavaScript doing something clever underneath the hood that you weren't expecting.

[-] masterspace@lemmy.ca 2 points 5 months ago* (last edited 5 months ago)

The explanation given to you makes it sound like == was deliberately designed to be a more convenient version of ===

I mean technically == was deliberately designed to be a more convenient version of other languages' == operator... Just specifically more convenient for light UI stuff since that was all JavaScript was supposed to be used for at the time (or all they thought it would be used for).

But give programmers a way to write and execute a small script and someone will eventually use that to try and write an emulator that emulates the computer it's running on, so the web evolved into more complicated applications, and then that convenience turned out to be wildly inconvenient, not to mention horribly unexpected for programmers coming from other languages, so then they added the triple equality to match other languages.

[-] sbv@sh.itjust.works 10 points 6 months ago

It's a slash-t in the comment. Maybe kbin has different rendering rules for comments?

[-] Limitless_screaming@kbin.social 1 points 6 months ago* (last edited 6 months ago)

Oh, in that case I replied to @MinekPo1 with my answer to that. BTW can you see the slash in: \t and "\t".

[-] sbv@sh.itjust.works 1 points 6 months ago
[-] Limitless_screaming@kbin.social 1 points 6 months ago* (last edited 6 months ago)

My bad. I just edited it. "\t" \t It's displaying correctly on Lemmy.world. So it seems like another Kbin only issue.

[-] MinekPo1@lemmygrad.ml 2 points 6 months ago

yeah but why is a single character string containing a tab equal to zero ???

[-] Limitless_screaming@kbin.social 2 points 6 months ago* (last edited 6 months ago)

That would be weird if a string containing a space wasn't equal to 0 " " == 0, but that's not the case in JS. If you think that "" and " " being equal to 0 is weird then I agree, but since they are, you should expect "\t" and "\n" to equal 0 too.

[-] Ephera@lemmy.ml 1 points 6 months ago

The == operator in JS will try to cast the things being compared and do all kinds of 'smart' assumptions about what equality means. This is why everyone uses === instead...

[-] 8bitguy@kbin.social 3 points 6 months ago

Unless you enjoy inviting the chaos.

[-] jtk@lemmy.sdf.org 7 points 6 months ago

Yeah, it's true. I knew all the other ones, had to put that one in the dev tools console to believe it. I was just happy to know === continues to be sane in that comparison.

[-] Blackmist@feddit.uk 5 points 6 months ago

You have to remember that the underlying principle of JavaScript seemed to be "never throw an error", even if what it's being told to do is weapons grade bollocks.

this post was submitted on 27 Dec 2023
719 points (98.5% liked)

Programmer Humor

18253 readers
1 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS