javascript is dumb

preview_player
Показать описание
JavaScript isn't cursed at all.....

Shoutout to Wat for inspiring me for over a decade now.

WTFJS SUPPORTS UKRAINE AND I DO TOO, YOU SHOULD CONSIDER DONATING

SOURCES

S/O Ph4se0n3 for the awesome edit 🙏
Рекомендации по теме
Комментарии
Автор

IIRC, NaN != NaN and -0 == 0 are part of the floating-point spec; most languages follow it.

sigstackfault
Автор

Hey Theo! The creator of the wtfjs repo here 👋

Thank you so much for making a walkthrough of my examples. I really enjoyed watching it, and I think we should definitely add a link to this video in the README. You’ve made it both entertaining and interesting to watch.

P.S: I noticed that you decided to cut out the repo banner asking for support for Ukraine. As someone still living in Ukraine, I experience the daily realities of missile and drone strikes, electricity outages, and all the other hardships that come with war. I, along with all Ukrainians, still need the community’s support.

DenysDovhan
Автор

the reason -0 exists is because of IEEE 754. it makes sense, Floating point is an approximation of the real number line. -0 represents that the very small number we want to represent is negatice

NithinJune
Автор

Ok 0*-1 =-0 is the funniest thing I have ever seen

Ironlionmn
Автор

half off video is Theo doesn't understand floating point numbers

guest
Автор

"Ask stupid question, get stupid answer" - JavaScript truthy, probably

theswordslay
Автор

I'm absolutely baffled that people expect "false" to be falsy. That's like expecting the string "JavaScript" to be a full Javascript interpreter. Just because you type out the characters doesn't mean it should behave the same way when represented as a totally different data type.

JackBond
Автор

I love how nearly all of these are impossible to do on accident if...
- You use TypeScript
- You don't do stupid shit

BellCube
Автор

Honestly, almost all of these weird things come down to the same root cause: type coercion. JS is just far too willing to coerce types, so if you code in a way that avoids it, you'll probably be fine; explicitly convert types yourself, don't rely on JS to do it for you.

Or, you know, use TypeScript.

caerphoto
Автор

+0 and -0 is not JavaScript thing. This is feature of floa ting point numbers internal format which is used by CPUs and GPUs for a very long time.

Zrgatul
Автор

The reason for NaN not being equal itself is that, if it was equal, then this would be true: Math.sqrt(-2) == Math.sqrt(-3) [NaN == NaN] where it is obviously false.

wojciech
Автор

"Why does -0 exist?", "Why is NaN so weird?", etc most of the weird things pointed out here are just actual parts of IEEE 754. IEEE 754 is what we usually call the floating point spec but its actually more than that its the floating point arithmetic spec and as such includes a bunch of things that are weird or confusing if you dont work on a very low level with floats. Things like NaN, -0, infinity and -infinity are all values that IEEE 754 have defined for good reason and their weird behaviours follow therefrom. NaN should not equal NaN since NaN is more of an error state than an actual value and are in spec returned from any "invalid" arithmetic operation (0/0, infinity * 0 that kinda thing). This is good! This makes float arithmetic algebraically complete that is every operation returns some value even if that value is kinda nonsense. This also means that there are multiple ways of getting NaN hence why it would make no sense for NaN to equal itself. Most other "weird" operations with these values have similar justifications. In my opinion the fault isn't in the fact that these values behave like this. They should behave like this there is a good reason they do and they don't (or at least they shouldn't but who knows what happens once JS type coercion is done destroying everyone reasoning abilities) show up in most code because they are edge case definitions. In general these values are very low level constructs so you could even argue about whether or not JS should even expose these directly instead of handling them in a more language appropriate way within the engine itself but it's too late for that now so. If you want a better understanding of why IEEE 754 is like it is go read the "Design Rationale" part of it's Wikipedia page or better yet go read the spec itself! It's a very interesting topic if you like these more low level things

nelind
Автор

Dude, seriously. Read the floating point spec. Most if those "errors" are in all programming language and for good reason

will_i_craft
Автор

Honestly, I feel these issues are overblown by people who dislike javascript for entire different reasons. Most of these boil down to "don't use operators on non-primitive types". I genuinely don't remember last time I would trip on any of these in last 15 years writing JS.

harou
Автор

> Why is `Number.MIN_VALUE` the smallest _positive_ value?

Because the smallest _negative_ value is `-Number.MAX_VALUE`. As simple as that. Again, simple floating point practices, common to all languages I am aware of. The ways how JavaScript treats floating point numbers is actually *consistent*, even though they may look weird and illogical (and appear "truly JavaScript-y").

ysakhno
Автор

"Array equality makes NO SENSE!!" and then array equality makes complete sense

SophiaWoessner
Автор

I'm frankly more surprised how little he knows about how javascript works tbh.

johanrg
Автор

Saying +"5" is a bad way to coerce and then suggesting parseInt is hilarious

haraldbackfisch
Автор

I swear there's a cron job somewhere that churns these articles out on a quarterly basis, complete with all the same convoluted examples.

chrsbll
Автор

12:50 Actually java also uses the to string. BUT ONLY WHEN ADDING SOMETHING TO A STRING. WHEN ADDING TO OBJECTS IT JUST TYPE ERRORS LIKE WHAT A NORMAL PROGRAMMING LANGUAGE WOULD DO.

rodrigoqteixeira