I Tried Defending EVERY Cursed JS Behavior

  Рет қаралды 55,438

Theo - t3․gg

Theo - t3․gg

13 күн бұрын

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
prytulafoundation.org/en/huma...
SOURCES
github.com/denysdovhan/wtfjs
x.com/t3dotgg/status/17674502...
www.destroyallsoftware.com/ta...
Check out my Twitch, Twitter, Discord more at t3.gg
S/O Ph4se0n3 for the awesome edit 🙏

Пікірлер: 345
@DenysDovhan
@DenysDovhan 10 күн бұрын
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.
@t3dotgg
@t3dotgg 10 күн бұрын
I did not mean to cut out the banner I just trimmed up the page to make it readable on my 720p screen! Donated $1k to Ukraine a week ago and will add a link in description to your preferred Ukrainian charity if you dm it to me (yt eats links)
@DenysDovhan
@DenysDovhan 10 күн бұрын
@@t3dotgg oh, got it. Sorry and thanks for your generous donation! DMed you on twitter.
@sigstackfault
@sigstackfault 11 күн бұрын
IIRC, NaN != NaN and -0 == 0 are part of the floating-point spec; most languages follow it.
@jnoded
@jnoded 11 күн бұрын
most languages simply don't have NaN or -0 as far as I know
@tschorsch
@tschorsch 11 күн бұрын
​@@jnoded Those are values defined by the floating point spec all processors implement. Any language that has a floating point type has to support them.
@von_nobody
@von_nobody 11 күн бұрын
​ @jnoded in other languages is "spelled" `-0.0`, JS simply drop `.0` and handle every thing as float until you force to` int` using some thing like `x | 0`
@ruroruro
@ruroruro 11 күн бұрын
Yeah, the real problem here is that JS doesn't really have integers. It has floating point numbers and then pretends really hard that they are integers.
@von_nobody
@von_nobody 11 күн бұрын
@@ruroruro I would not say "pretends" as if you try enough that you will get real `int`, other wise `asm.js` would not work.
@astral6749
@astral6749 11 күн бұрын
This makes Dreamberd look credible
@Seedwreck
@Seedwreck 11 күн бұрын
I hope you find the charity DB supports credible
@NithinJune
@NithinJune 11 күн бұрын
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
@PaweMateuszBytner
@PaweMateuszBytner 11 күн бұрын
+0 and -0 in floats are more like right and left limits. Floats simply don't have *real* 0. Real 0 inversed is always undefined (in a NaN sense, not js-undefined sense) while left and right limits are inversions of infinities
@hevad
@hevad 7 күн бұрын
@@PaweMateuszBytner the reason why there's -0 is because unlike integers in 2's complement, the mantissa in a floating point type, is stored as an unsigned integer, and there's a separate bit for sign. So, if you have a bit pattern for zero, then if you just flip the sign bit, well, you have a bit pattern for -0 (-0.0, actually). CPUs will take care of it, tho, and will compare as equal one to the other.
@BellCube
@BellCube 11 күн бұрын
I love how nearly all of these are impossible to do on accident if... - You use TypeScript - You don't do stupid shit
@Jiyoon02
@Jiyoon02 Күн бұрын
Well, yes, but also, isn't the whole point of using a high-level language to prevent coders from doing stupid shit. JS doesn't seem to fulfill that, does it. Not that it's surprising or anything.
@BellCube
@BellCube Күн бұрын
@@Jiyoon02 I don't disagree-JS just had very naïve design goals w/ dynamic typing and such.
@Ironlionm4n
@Ironlionm4n 11 күн бұрын
Ok 0*-1 =-0 is the funniest thing I have ever seen
@thomassynths
@thomassynths 11 күн бұрын
Not a JS issue. It's called IEEE754
@Ironlionm4n
@Ironlionm4n 11 күн бұрын
@@thomassynths regardless it’s funny 🤷🏻‍♂️
@nitsanbh
@nitsanbh 11 күн бұрын
@@thomassynthsyeah, other languages just hide the negative sign when you print
@PP-ss3zf
@PP-ss3zf 11 күн бұрын
i feel like its actually intuitive... 0 * positive int = 0, 0 * negative int = -0
@carlpittenger
@carlpittenger 11 күн бұрын
​@@nitsanbhno, every other language i know evaluates literal -0 to integer 0. but yes, 0.0 and -0.0 are different values according to IEEE
@caerphoto
@caerphoto 11 күн бұрын
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.
@TimeLemur6
@TimeLemur6 11 күн бұрын
Throwing stuff in strings in general (vs a more fitting type) has always given me the heebie-jeebies, and it seems like JS is super eager to do just that.
@BellCube
@BellCube 11 күн бұрын
This comment right here. This. This. This.
@seanchen9771
@seanchen9771 10 күн бұрын
just be somewhat aware of types and readability then all these weird code will not appear
@zanebartlett8004
@zanebartlett8004 9 күн бұрын
@@seanchen9771 Just rely on the every single person in the team of people you work with throughout time to be this way and your codebase won't be awful? I'd rather not.
@seanchen9771
@seanchen9771 9 күн бұрын
@@zanebartlett8004 what you gonna do about it. Rewrite the whole codebase? I mean people throw objects in for boolean checks every now and then right? If the project youre working on started with js you are most likely stuck with it and most people would not do obscure things mentioned in this video. If it happens it happens there is nothing you can do about it.
@guest7329
@guest7329 11 күн бұрын
half off video is Theo doesn't understand floating point numbers
@hevad
@hevad 7 күн бұрын
Exactly. But also, the author of the document. I find it weird that they dwell so long on `NaN` not being equal to itself. Is as if no one has elementary computer org classes anymore. JavaScript makes it a bit quirkier because all of the automatic promotions/coercions between objects, arrays, numbers, and booleans, then somewhere some silent `NaN`s pop up and act weirdly. But the rest of the `NaN` behavior is standard across the board on any language.
@Z3rgatul
@Z3rgatul 11 күн бұрын
+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.
@leonoliverrmusic
@leonoliverrmusic 11 күн бұрын
That is in deed correct!
@carlpittenger
@carlpittenger 11 күн бұрын
the 0 and -0 literals meaning 0.0 and -0.0, respectively, are, however, a javascript thing. even python got that right. hence bigint being tacked on
@Hyperboid
@Hyperboid 11 күн бұрын
NaN != NaN is also a floa ting point quirk
@dealloc
@dealloc 11 күн бұрын
@@carlpittenger Any scripting lan guage that follows the ECMAScript spec (i.e. ActionS cript and JScript) represents Numbers as 64-bit floating points. not just JavaScript.
@carlpittenger
@carlpittenger 11 күн бұрын
@@dealloc jscript is dead and no one uses actionscript anymore. i'm obviously in this context meaning (and in fact most people in any context mean) ecmascript implicitly
@will_i_craft5555
@will_i_craft5555 11 күн бұрын
Dude, seriously. Read the floating point spec. Most if those "errors" are in all programming language and for good reason
@harou22
@harou22 11 күн бұрын
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.
@EdwinMartin
@EdwinMartin 10 күн бұрын
Exactly!
@kirbykilledgod
@kirbykilledgod 9 күн бұрын
As someone who doesn’t use Js these seem a pain to deal with or could cause completely stupid bugs if you ain’t paying attention like what on earth is going on here 30:47
@wojciech567
@wojciech567 11 күн бұрын
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.
@DimkaTsv
@DimkaTsv 4 күн бұрын
But then isn't it allows for Math.Sqrt(-2) == Math.Sqrt(-2) [NaN == NaN] false Where it should be true?
@NithinJune
@NithinJune 11 күн бұрын
12:02 What do you mean why?? thats literally your epsilon
@sofia.eris.bauhaus
@sofia.eris.bauhaus 5 күн бұрын
well it's just badly named…
@dtkedtyjrtyj
@dtkedtyjrtyj 11 күн бұрын
These kind of videos serve mostly to show off the ignorance of the person looking at the examples. In the nineties and naughties, people had this silly idea that languages shouldn't trigger error messages when people did something wrong, but instead try to do what the programmer intended. I think that was a reaction to the cryptic core dumps and errors of languages of the seventies and eighties. People thought error messages was the problem so they avoided them. This is common to all languages of that era, JavaScript, PHP (which has exactly one good feature), Ruby, Python, CoffeeScript, jQuery... Some are worse than others, but all are infected with that mindset. And then people usually go on to complaining about floating point arithmetic to just _really_ show off their ignorance.
@ysakhno
@ysakhno 11 күн бұрын
> 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").
@theswordslay3542
@theswordslay3542 11 күн бұрын
"Ask stupid question, get stupid answer" - JavaScript truthy, probably
@chrsbll
@chrsbll 11 күн бұрын
I swear there's a cron job somewhere that churns these articles out on a quarterly basis, complete with all the same convoluted examples.
@danhorus
@danhorus 11 күн бұрын
Mandatory comment complaining about blocks being called closures in this video
@johanrg70
@johanrg70 11 күн бұрын
I'm frankly more surprised how little he knows about how javascript works tbh.
@ivonakis
@ivonakis 11 күн бұрын
TLDW Every few years someone makes this video. These issues do exist but they don't seem to manifest in real code.
@HalfAsleepSam
@HalfAsleepSam 11 күн бұрын
"Array equality makes NO SENSE!!" and then array equality makes complete sense
@Seedwreck
@Seedwreck 11 күн бұрын
Honestly confused on why everybody hates this weird JS, you never run into these issues if you write good code
@thesunrock
@thesunrock 11 күн бұрын
Hype
@woofcaptain8212
@woofcaptain8212 11 күн бұрын
All fun and games till you have to touch a 30k line codebase that uses == everywhere
@MrTomyCJ
@MrTomyCJ 11 күн бұрын
@@woofcaptain8212 If you use == to compare potentially unknown stuff that can lead to unexpected scenarios, then I'd say that's bad code.
@Seedwreck
@Seedwreck 10 күн бұрын
@@woofcaptain8212 Well, if you aren't dealing with data that isn't very specific like data with strings only, then yes
@nelind3
@nelind3 11 күн бұрын
"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
@haraldbackfisch1981
@haraldbackfisch1981 11 күн бұрын
Saying +"5" is a bad way to coerce and then suggesting parseInt is hilarious
@kartikjha833
@kartikjha833 11 күн бұрын
why?
@untlsn
@untlsn 11 күн бұрын
@@kartikjha833 You have explenation on video for example you shoud always set type of parsing as second argument, because not all runtimes have it the same
@CrazyWinner357
@CrazyWinner357 11 күн бұрын
Imagine a language that you need a fucking phd to convert a string to int, lol
@nathnolt
@nathnolt 10 күн бұрын
He should have use Number("5"). it actually is the same thing as +"5". but Number actually is a declarative way to say what you're doing.
@untlsn
@untlsn 10 күн бұрын
@@CrazyWinner357 fortunetry in lower languages like C or rust converting to int is a breeze Yes? Yes?!
@teasdaye
@teasdaye 11 күн бұрын
2:32 Honestly, when the first example here is about asking "why?" does the order of operations matter in a programming language, I'm really not sure what the point of this video is
@3limin4t0r
@3limin4t0r 11 күн бұрын
Regarding the empty slots in array: They denote a missing index it's not a different type of `undefined`. Say you have the array `const sparseArray = [/* empty */, "1", 2]`, then you can check if a slot is empty by doing `0 in sparseArray` if the array doesn't have the property `0` it indicates an empty slot in index position `0`. The reason why the return type is a normal undefined is because if you access an attribute that's not there you simply get `undefined`. For example `const obj = { a: 1 }` then `obj.b` will yield `undefined`. Just like `sparseArray[0]` yields `undefined`. You are accessing a property that doesn't exist. If I would do `delete sparseArray[1]` then index `1` is now also an empty slot.
@turbdonkey
@turbdonkey 11 күн бұрын
I thought the double not operator converts values to their boolean equivalent so !!"false" would be true because a string is truthy. !!"" would be false because an empty string is falsy. Same thing for the !!null, that would be false because null is falsy. The explanations around all the double nots seem to be convoluted at best.
@dealloc
@dealloc 11 күн бұрын
No, the single bang (unary logical negation) not only coerces the value into a boolean, but also negates the operand after coercion. This is boolean logic and it is equivalent to `Boolean("string") === false`. Applying two bangs just negates the negated value i.e. `(Boolean("string") === false) === false` It's the same for unary plus/minus operator (+, -). It coerces into a number like (Number("123string")) and evaluates it. So -`"-12" === 12`. This is different from the addition operator, which will coerce both operands into primitives before evaluating the expression. If one side is a string, the other is coerced to a string as well. Otherwise if both are numbers it will perform an addition.
@abtix
@abtix 11 күн бұрын
I agree with most things but some stuff is really simple, such as 3:07, and I feel like you may be overexaggerating this a bit. !!"false" == !!"true" being true is completely normal. A non empty string is truthy. The first ! makes it false, and the second flips the false and makes it true. There is nothing wrong with this, and yet you make it seem like it's very strange, and that we are in "hell". 4:57, When checking if a boolean is equal to a string, it obviously wouldnt return true. Now given my previous point, you might then confuse why !!"true" is true, and that is because ! converts it to a boolean. So it goes !!"true" -> !false -> true, which also explains 5:03. I wouldn't say that is "dumb". 9:20 Now I know this one isn't intuitive to understand, but I will try to explain it in a way that hopefully the confusion would go away. I believe the reason why null == false is false but 0 == false and "" == false is true, is because null is loosely connected to undefined (which is also why at 10:28, document.all == null is true, while document.all == undefined would also be true, but document.all == false is fasle). Undefined is when the value isnt assigned, and null is when its been assigned but has no value, and so there is no type coercion. Strings and numbers go through type coercion but null doesnt, since it's not really a type. All you need to remember is that there is no type coercion for null and undefined and that should make sense of this. People should not be told these are challenging or strange. You can make simple things become difficult by simply pretending that they are. Many people including myself look up to you, and so I think we would all appreciate it if you would actually pause to think about these things instead of hopping on the "Yep there it is, JavaScript is dumb".
@Zedoy
@Zedoy 11 күн бұрын
Agreed
@user-oj7uc8tw9r
@user-oj7uc8tw9r 11 күн бұрын
It's convoluted BS that, in my opinion, hardly anyone uses and would cause unintended side effects. There is no need to use boolean negation on a string, it makes no sense and it just causes massive amounts of type confusion. It is not normal, in any sense, to boolean negate a string to a boolean value because what if you never intended said string to be a boolean, but simply a string with the word true or false? This is why I hate dynamically typed languages. They treat everything as a variant essentially which causes all these conversion side effects that you never intend.
@abtix
@abtix 11 күн бұрын
@@user-oj7uc8tw9r Yeah I mean there is literally no reason anyone would be using double negation like that. I find myself using boolean negation on strings in situations such as: if (!node.textContent) throw Error(“Node has no content”) Or for double negation: function isValidName(name) { return !!name } But the above could also just be written as: return name != “”. For me, I think projects that I faced some side effects were the ones that I used a ton of APIs with varying and sometimes unpredictable response values. And even then, I never really found myself thinking JavaScript was the problem, it was just that I didn’t know enough. With what I know now, I would make fewer mistakes and likely waste little to no time dealing with these side effects. I think all of this JS hate is pointless, it’s still a very powerful language regardless. If you write good code, it would be easy to debug the side effects if they ever occur
@user-ky6cc9bq4b
@user-ky6cc9bq4b 11 күн бұрын
​@@user-oj7uc8tw9r It's actually normal to use negation on a string to check if it's empty or not in a dynamically typed language. That's what you would expect: an empty structure is falsy. Tho I prefer the C++ way, just a simple empty() mehod to check.
@user-oj7uc8tw9r
@user-oj7uc8tw9r 11 күн бұрын
@@user-ky6cc9bq4b Ive never see it done in a statically typed language. It would fail in C#, fail in Java, fail in C. You dont use a negation operator to convert a "false" or "true" string to boolean. You convert it to boolean through a cast or parse method, then use boolean operators on it. That makes sense because a string is not a boolean type. This way, Javascript just assumes the string value should be boolean and converts it as a side effect.
@Roundaround
@Roundaround 11 күн бұрын
Really this just shows that the mentality of "sensible defaults" rather than erroring is a terrible idea for language design and programming in general
@benebene9525
@benebene9525 11 күн бұрын
nah it just shows that js defaults are everything but sensible XD
@MrTomyCJ
@MrTomyCJ 11 күн бұрын
@@benebene9525 But maybe there is just NO way to have sensible defaults: they will always result in unsensible results in some scenario.
@zettca
@zettca 10 күн бұрын
yeah; websites blowing left and right would have been so much better! or, you know... don't use type coercion - just an an extra =
@ZipplyZane
@ZipplyZane 11 күн бұрын
If you don't get -0, then you can't understand floating point. It's not a JavaScript thing at all. All floats are approximations. They have limited precision. And sometimes it matters if the result you get is slightly less than 0 or slightly more than 0. NaN !== NaN makes sense, too. NaN is a result you should not get. If two expressions fail, you don't really want them to be equal to each other. And I get why "false" != false. It's normal to check if a value has been input by doing "if (textbox)". It would suck if that failed because the user typed in "false." The idea that any non-empty string is true makes sense to me. I only find it weird when the DOM coerces false or undefined into "false" and "undefined". That's dumb, and should be the same as assigning the empty string. And of course string- number equal not a number. There is no defined operation for subtracting strings. What else could it return?
@SJohnTrombley
@SJohnTrombley 11 күн бұрын
"the falseyness occurs before the number coercion...why?" I think you already know why, it's just about evaluation order. The real questions are why was it decided that the empty array is truthy and why was it decided that == coerces things to numbers.
@rafagd
@rafagd 11 күн бұрын
-0 and 0 are warts of IEEE754 Floating numbers and as such they exist in most programming languages. There's no -0 in integer numbers.
@antopolskiy
@antopolskiy 11 күн бұрын
Repo: Consider this step-by-step Theo: "I don't want to consider this step by step" cracked me up
@nage7447
@nage7447 11 күн бұрын
15:57 sometimes I think that He doesn't know what he talking about ) so, basically what happened, first array is empty and when you try to access nonexisting key you will get undefined the same that you store in second array under existing key you can not get "empty" value from array
@bdafeesh
@bdafeesh 11 күн бұрын
"I hate everything. I'm going to become a farmer." I think about this often...
@ben65797
@ben65797 11 күн бұрын
Prototype helps so much when I need an insert function bc somehow js doesn’t have it yet😢. Edit: nvm I bothered to do research but still, why is it an extra arg on splice with 0 in the delete count?
@gFamWeb
@gFamWeb 11 күн бұрын
I'm so early, most of the comments are spam!
@Dekutard
@Dekutard 11 күн бұрын
isn’t that the usual?
@ProSureStrings
@ProSureStrings 11 күн бұрын
Lol fr
@bes1desme
@bes1desme 11 күн бұрын
Fr
@thepeer
@thepeer 11 күн бұрын
I'm not spam
@ProSureStrings
@ProSureStrings 11 күн бұрын
@@thepeer ok
@radekmojzis9829
@radekmojzis9829 11 күн бұрын
Integer arrays being sorted alphabetically is remarkable...
@Seedwreck
@Seedwreck 11 күн бұрын
I feel like Theo is the kind of guy that if he ever used ActionScript3, he'd be stuck using it since its got most modern syntax features (E4X, JS typing, Embedding file) in an old language
@Gnarrbarr
@Gnarrbarr 11 күн бұрын
your sarcasm actually breaks me
@mchisolm0
@mchisolm0 11 күн бұрын
Pretty rough. Grateful someone else went through it, not me.
@nein3405
@nein3405 11 күн бұрын
"why is there a -0" thats because how the sign is stored in memory.
@vilemjenis4973
@vilemjenis4973 11 күн бұрын
I'm wondering... What is I define a toString method which does not return a string? How will the + operator work then? Just try it: const a = { toString: ()=> 1, }; const b = { toString: ()=> 3, }; a + b;
@_Verac
@_Verac 11 күн бұрын
It's weird because most people don't understand typecasting that's done by js when == is used instead of using ===.
@NithinJune
@NithinJune 11 күн бұрын
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 negative
@skyjumper4097
@skyjumper4097 2 күн бұрын
the "wat" talk honestly summarizes javascript pretty well
@lcschannel1492
@lcschannel1492 11 күн бұрын
I'm new to JavaScript and learning, I like it so far, coming from Java, But this looks scary lol! So should I even go further or do I still have time to change lol.
@kimailis
@kimailis 11 күн бұрын
you can go further, i doubt you will ever have to face all those weird shenanigans during development.
@woofcaptain8212
@woofcaptain8212 11 күн бұрын
Learn other languages for the backend. JS and TS add so much complexity compared to the nice dev environments in kotlin, go, hell even java
@theswordslay3542
@theswordslay3542 11 күн бұрын
since you're used to java, i think you'll be way more careful about variable type that the usual JS shenanigans won't be much of an issue. you could also easily switch to TS if JS type weirdness gets to you, TS is just JS with type safety, no need to relearn JS syntax.
@PaweMateuszBytner
@PaweMateuszBytner 11 күн бұрын
Well, if you watch out for what types you are using (it is tedious with dynamic types and implicit casting everywhere) and don't throw random operators at random variables, there is nothing to be scared about. Also please don't repeat OPs mistakes and just 1. read the documentation, 2. get some grasp about higher level math and actual computer science.
@tombnomb2938
@tombnomb2938 11 күн бұрын
Bro I genuinely didn't know abt the sort thing at 40:49 and now my code doesn't randomly function in dumb orders
@menegatmarcelo
@menegatmarcelo 11 күн бұрын
Its funny how TS devs talks about JS! Bro, you use a linter and call it language! Comm!
@aztracker1
@aztracker1 11 күн бұрын
You can attach properties to String objects but not native string instances. I've used this for templating meta information for database string templates.
@josefaguilar2955
@josefaguilar2955 11 күн бұрын
Your reaction thumbnail is spot on.
@redvortex_p
@redvortex_p 11 күн бұрын
alternative title: theo grunting for 45 minutes straight
@misdelivereddishwasher1011
@misdelivereddishwasher1011 10 күн бұрын
thank you for inspiring me to watch actual coding channels who know what they're talking about and also to join your nemesis as a member, he needs more funding to do what he's doing for the community :p
@orionh5535
@orionh5535 11 күн бұрын
Javascript has a reason for most of its weirdness. The reason some design decisions are criticized is that much like required reading in highschool, if you work with the web at any point, you have to deal with javascript. People just grow resentful they have no choice to use it, and making fun of certain features become memes. Because of its position as the main programming language of the web, it can't really do breaking changes, and undoing certain core design desicions isnt feasiable. When it was created, its creator had no idea of the scope it would eventually cover, and since the community grew so huge, contributors had to balance giving everyone enough features so that everyone was only half angry. But yes it is fun to make fun of all the quirks of the language, just don't forget that there is context to all of the design decisions you don't love. Also python, get some brackets, ruby, not everything has to be magic, Php, you're cool now.
@beaticulous
@beaticulous 11 күн бұрын
We should break things more often.
@m4rt_
@m4rt_ 10 күн бұрын
I would assume (I haven't looked at the actual implementation) that values are stored in a way similar to this: struct Value { bool is_empty; enum { VAL_UNDEFINED; VAL_NULL; /* ... */ } type; void *value; } so when you set no value, it just sets is_empty to true, but when you set a value manually, it sets it to false, and then sets the type to undefined. I'm not sure why undefined doesn't just always set it to empty, or empty is just the undefined type, but maybe its because JS is old and people rely on it now.
@rodrigolaporte274
@rodrigolaporte274 11 күн бұрын
The more crazy js is, the more I like it. Awesome video!
@mcfincher29
@mcfincher29 11 күн бұрын
I've been developing for a living for a few years now. Mostly building analytic tools in Python, and dabbling in low level stuff like Rust and C... As someone who knows nothing about Javascript, but has seen many videos like this before, I'm struggling to understand why anyone would ever learn JS in the first place. Is there any good reason? I want to expand my toolkit as much as I can, but I've avoided JS like a plague so far.
@kimailis
@kimailis 11 күн бұрын
maybe because browsers dont understand python.
@tylisirn
@tylisirn 11 күн бұрын
Because if you want to develop on the web, you have to use JS. The browser runs JS and nothing else. (There is Webassembly, but it's not used for general frontend stuff, it's quite specialized use cases only.) And since you have to use JS on the frontend, using it on the backend as well (Node.js) is a common option. But on the backend you do have other options (PHP, Python, etc...)
@mcfincher29
@mcfincher29 11 күн бұрын
@@kimailis Didn't know it was a browser exclusive thing. I just write backend stuff.
@mcfincher29
@mcfincher29 11 күн бұрын
@@tylisirn Interesting. I didn't know it was the only option for browsers. I don't do any sort of front end or UI work at all, CLI stuff only for me, so probably not something I need to dive into any time soon.
@kimailis
@kimailis 11 күн бұрын
@@mcfincher29 yeah and because of that it has a very large community, which grows bigger with options of backend development with node.js and mobile development with react native. all of that makes javascript to be a very versatile language.
@ersetzbar.
@ersetzbar. 11 күн бұрын
I do have some cases where I add numbers in JS like if I want to have atleast 2 out of 4 things to be truthy I can cast them to bools add them and check if the sum is greater or equal to 2
@Requiem100500
@Requiem100500 6 күн бұрын
Someone looked at DreamBerd spec and thought "hmm not scuffed enought", and then invented JavaScript
@RandomGeometryDashStuff
@RandomGeometryDashStuff 11 күн бұрын
21:59 which javascript engine `{} + {} // -> '[object Object][object Object]'`? I get NaN in librewolf 127.0.1-1
@pepkin88
@pepkin88 11 күн бұрын
put it in the parentheses, so that it is parsed as an expression: `({} + {})`
@RandomGeometryDashStuff
@RandomGeometryDashStuff 11 күн бұрын
@@pepkin88​how did they get `{} + [] // -> 0` then? `({} + [])` evaluates to '[object Object]'
@retzerR
@retzerR 8 сағат бұрын
I think Google has a brain implant in me, I was just thinking about how cursed JS is, just a random thought, and I scrolled half a page, and here's this video. It's not even like I watch heaps of this channel 😂
@AvanaVana
@AvanaVana 11 күн бұрын
12:00 This is an important concept in math. I was just reading about Kakeya’s Needle Problem today, and the goal in it is to minimize the area needed to sweep a needle of zero-width 180°. Various mathematicians found ways to minimize the area needed that approach zero at the limit, but the area can never actually _be_ zero. This would be a perfect application of Number.MIN_VALUE. The limit would be equal to it, and it would be greater than zero, for good reason. Meanwhile, negative infinity would be smaller than any number, and negative Number.MIN_VALUE would be the largest negative number before zero.
@linuxguy1199
@linuxguy1199 11 күн бұрын
Fun fact, -0 is actually part of the FP standard, a lot of languages have it! I've specifically used it as a special flag in games I've made.
@rohitkharche7562
@rohitkharche7562 11 күн бұрын
It seems like there is still an opportunity to create an npm package to warp these comparisons into custom comparators, just to break out of this madness
@it_is_random
@it_is_random 11 күн бұрын
my mind is going blank
@matthewtanous7905
@matthewtanous7905 9 күн бұрын
It’s so weird that toString unwraps an array of one element. No other language I know of does that.
@rickwoods5274
@rickwoods5274 5 күн бұрын
i love how about 3 seconds into a video where he tries to pretend to defend dumb js crap, he just stops pretending and immediately wants to roll over
@ShadowKestrel
@ShadowKestrel 11 күн бұрын
so many of JS's wonky-ahh casts makes me feel smug as someone who primarily uses ocaml (which is so strictly typed that float and int addition have to have separate symbols)
@sukitta2
@sukitta2 11 күн бұрын
-0 is just the "ain't no" of JS.
@TheFerdi265
@TheFerdi265 11 күн бұрын
About the new String thing: you can call the String and Number constructor with and without new: without new, it works just like you would expect, it converts the argument to the respective type, and typeof will give you a reasonable result. with new, you get a "String object" or "Number object", which is basically like a string or number, except that you can actually override functions and stuff, as you've shown with your "this is the wrong string" example.
@TheFerdi265
@TheFerdi265 11 күн бұрын
you can even create unrelated properties on the String/Number objects if you created them with new: > s = "hello" 'hello' > s.a = 42 42 > s.a undefined > s = new String("hello") [String: 'hello'] > s.a = 42 42 > s.a 42 >
@BattyBest
@BattyBest 11 күн бұрын
The guy who made the floating point standard must have been high or something, they single-handedly caused 10% of all bugs in webdev...
@andrew_ray
@andrew_ray 11 күн бұрын
35:00 This is why you should turn off "allowUnusedLabels" in your tsconfig when you write TypeScript. () => { hello: "world" }; will report an error with that option set to false.
@szirsp
@szirsp Күн бұрын
37:40 so are you saying that the assignment operator evaluation order is left to right? foo.x = foo, then foo = {n:2} what about this then: var y=1; var x = y = 2; ???
@clementj2588
@clementj2588 11 күн бұрын
I have 2 uses for the double equal: - x != null // check if variable x is defined - x == y // check if values are equal, when you KNOW they are either a number or a string, like for checks with input values
@Lemmy4555
@Lemmy4555 10 күн бұрын
x == null // check if x is nullish (null or undefined)
@HeroOfHyla
@HeroOfHyla Күн бұрын
In the Godot game engine, a freed object == null, but is truthy. Assigning a freed object to a variable _sometimes_ throws an error, and a freed object inside a closure gets turned into a null. And of course you also need to check if an object is queued to be freed at the end of the frame but hasn't actually been freed yet. I think some of this is changing in 4.3.
@sofia.eris.bauhaus
@sofia.eris.bauhaus 4 күн бұрын
patching the Number prototype has a neat application: you can make numbers iterable (counting 0 to n-1), so you can "for...of" them or use them everywhere iterables can be used. i also added some forEach, map and reduce methods. for numbers that aren't positive intergers, the behaviour isn't quite so obvious but your also unlikely to use them for those. :P i get why expanding builtins is probably not something you should do in a professional setting, but it's a very nice feature to use and it may be a cool in other languages.
@scullyy
@scullyy 10 күн бұрын
I think I would have given up on learning programming had I started with JS.
@ivanbragin7932
@ivanbragin7932 11 күн бұрын
I like how it started as "F this language" and then past 20 mins suddenly "it is cursed but it does make sense". See? That's how JS will get you
@morgan0
@morgan0 7 күн бұрын
"why is the internet slow?"
@cwagnello
@cwagnello 11 күн бұрын
I love reJular expressions lol
@patrickaikonia853
@patrickaikonia853 11 күн бұрын
The hell I went through coming from Java and C to javascript. The syntax didn't make sense at all simply because of those small unintentional bugs until I started working it it from the beginning forgetting all the programming concepts, learning the syntax and then reapply those concepts. For a person starting with JavaScript its okay but coming from a statically typed language, you will hate it.
@thesunrock
@thesunrock 11 күн бұрын
What looks like a duck, quacks like a... You entered the JS world with the knowledge that is not completely transferable and applicable. Most devs with different backgrounds make assumptions about how JS works. When they get hoisted on their own petard, they hate the language. ECMAScript spec (and some other standards) addresses most if not all the weird stuff shown here. It is 2024, Theo's take on this topic is archaic.
11 күн бұрын
NaN != NaN is part of IEEE spec and same in other languages. It makes sense since something like 0/0 == parseInt(“hello”) would be crazy, and make debugging harder. Classic frontend script kiddie to blame standard floating point behaviour on js ;)
@Mystic998
@Mystic998 11 күн бұрын
Barring potential weirdo string conversion stuff, no comparison operator with NaN returns true other than NaN != x, where x is any floating point value, including NaN, which is about as close to NaN being non-comparable to other Numbers (including itself) as you can get without introducing null or undefined as a possible outcome. Basically this lets you do operations with "infinity" without having to worry about spitting out true when comparing two indeterminate forms that have different limiting values. The question comes down to, "How do you evaluate the truth value of something like (2x - 4) / (x - 2) == (x^2 - 4) / (x - 2) at x = 2?" The only way to reasonably do it is actually taking the limit of both sides (in this case 2 != 4). And you could do this programmatically with some effort and probably be right most of the time, but boy is it easier to just say, "Lol no."
@PaweMateuszBytner
@PaweMateuszBytner 10 күн бұрын
​@@Mystic998 With regular languages like JS you just don't look at any statement with comparison operators and treat it as an equation to solve, because those are just simple operators that are evaluated in particular order. You know, CPUs don't think and don't learn calculus. Solving equations is a task for specialised tools that either have actual concept of a limit and can approximate it numericaly or even can do symbolic math and give you general solutions. And indeed, in JS (and C# and any reasonable non-specialised language that does floats), you just say "lol, no". More accurately, you evaluate to NaN that can stand for undefined value
@patrickoberholzer4278
@patrickoberholzer4278 11 күн бұрын
A lot of this didn't even seem that bad to me, knowing JS.
@abhisheknavgan6829
@abhisheknavgan6829 11 күн бұрын
it was very fun, I am a beginner in JS (with a year of exp, nothing imo) and learned a lot
@chainingsolid
@chainingsolid 10 күн бұрын
Given how much of JS's weirdness is actually just IEEE754 abuse (like .1+.2 = .30..04). Go look up some talks on it!
@mudi2000a
@mudi2000a 10 күн бұрын
Funny how everyone was complaining about Perl when JavaScript has at least the same level of “unexpectedlyness” as Perl especially regarding strange automatic type conversions.
@chainingsolid
@chainingsolid 10 күн бұрын
8 minutes in and I'm wondering if Theo knows of IEEE754's weird quirks... But to be fair JS would be the language that could falsely claim credit for them....
@linuxguy1199
@linuxguy1199 11 күн бұрын
As a C developer, I genuinely have no idea how you deal with those atrocities. I think I'd just quit programming. This video actually made me have a lot more respect for web developers. Hats off, seriously.
@s3rit661
@s3rit661 11 күн бұрын
And people are surprised google's js programmers created dart? And you know what? It has Java syntax, yep, Java will never die
@zwanz0r
@zwanz0r 11 күн бұрын
I've done so much TypeScript' now... I can't even reason about js anymore
@Seedwreck
@Seedwreck 11 күн бұрын
Wasn't even about TS, and is subset
@frechjo
@frechjo 8 күн бұрын
The String.split() behaviour is correct (at least the one shown here, it might do something weird in some other cases, it's JS after all): - There are empty strings between each character (conceptually), so splitting on "" must return an array of every character on the string. Since you are calling it on an empty string, that result will be empty. - Splitting by something not in the string should return nothing, because there's nothing to each side of that separator ( the case for "".split(" ") ). These are just edge cases, but the behaviour is logical.
@themprsndev
@themprsndev 6 күн бұрын
Man, being a C# & Blazor dev is so much simpler, almost equals to becoming a farmer lol, it's complete inner peace
@Prod-23
@Prod-23 11 күн бұрын
The fact Typescript even exists is a red flag for Javascript.
@jeffreyblack666
@jeffreyblack666 10 күн бұрын
I would say the explanation would make more sense if it did it step by step, rather than just jumping straight to number. i.e. why does [] become 0? My understanding (which may be entirely wrong) is that it converts it to a string, and then parses that string as a number. This means if it is an empty array, it will be an empty string, which returns 0. If it is an array with 1 element, it will return 0 if that element is the empty string, whitespace, null or undefined; a number if that element is a number including a string representation of a number, and NaN otherwise. If it is an array with 2 elements, it will return NaN. For added cursedness: While [] == !![] returns false, [1] == !![1] returns true. And while [1] == [1] is false, [1] == ![1] is also false.
@flamenate
@flamenate 11 күн бұрын
30:56 "I like spreading"💀 last video was "fuck am I a bottom?". are you trying to tell us something theo?
@crism8868
@crism8868 5 минут бұрын
23:13 wouldn't monkey patching being a thing open up all sort of vulnerabilities? It is not just cursed it is a real danger 😱 how is this allowed ??? Why don't browsers just freeze prototypes ??????
@levsonc
@levsonc 10 күн бұрын
There is a complicated table of how == equality works. But the truth is… you don't need it in reality, unless you're doing something strange. If you're comparing primitives they would be coerced to string or number depending on what you have passed in. So, it's handy to check for example == 2, when your backend may send "2" with no need of manual coercion Number('2'). I'd recommend to use strict comparing === to *true* or *false* , but that's a rare case, especially after defaults where introduced, and one can do something like function foo(bar = true) { if (bar) … // could be “if (bar !== false)” in the old days } Objects are compared their to strict identities between them, no matter are you using == or ===. [] != [] is same as [] !== [] and is true, since you're comparing two different freshly created arrays here. *null* and *undefined* are equal only to self, as already noted. So it's safe to compare anything with them. (Usually != null is handy and shorter.) The unexpected part begins when you're comparing any type of object to primitive, so it's a bad idea to do. Just don't do it. Why would you? Why would one compare *false* and an array? It's that case when explicit better than implicit. E.g. by using .toString(). But still it's quite a rare case too. Usually, objects compared to be same or being nullish. So, that WAT part is quite overhyped really. Yep, one cannot use + to concatenate arrays like in Python, [...array] spread or .concat() should be used for that. But you learn it when you learn basics about arrays and their methods. No big deal.
@Matt23488
@Matt23488 11 күн бұрын
The promise flattening is unsurprising if you've ever used fetch() on an API. The .text() and .json() methods of the response return promises. Code I commonly saw and wrote (before async/await) was like fetch(...).then(r => r.json()).then(myJson => { ... } ) and myJson would not be the promise, it would be the resolved value.
@jeffreyblack666
@jeffreyblack666 10 күн бұрын
The fun stuff for NaN is equality in the simple sense of numerical value. If the result of a math operation is undefined, then you get NaN. By definition, this cannot equal any other number. Just like infinity cannot equal infinity. As you cannot make a comparison between their values. For 0, by definition -0 is a different object to 0. But their values are both 0, so if you compare them by value, even using the strict equality operator, the result is true.
@MrAustindanderson
@MrAustindanderson 20 сағат бұрын
Whoever invented nodejs are the ones who are truly unhinged, taking this cursed frontend language and deciding to use it for backend
@tippififestarr6519
@tippififestarr6519 11 күн бұрын
even though its totally obvious you are being sarcastic and having a fun, i still really appreciate your hint here at 1:45
I Parsed 1 Billion Rows Of Text (It Sucked)
39:23
Theo - t3․gg
Рет қаралды 83 М.
100+ Linux Things you Need to Know
12:23
Fireship
Рет қаралды 36 М.
Osman Kalyoncu Sonu Üzücü Saddest Videos Dream Engine 170 #shorts
00:27
LOVE LETTER - POPPY PLAYTIME CHAPTER 3 | GH'S ANIMATION
00:15
Жайдарман | Туған күн 2024 | Алматы
2:22:55
Jaidarman OFFICIAL / JCI
Рет қаралды 1,6 МЛН
I Can't Believe We Did This...
00:38
Stokes Twins
Рет қаралды 60 МЛН
So I've Been Trying Other Languages...
30:59
Theo - t3․gg
Рет қаралды 58 М.
Interview With A Sr JavaScript Dev | Prime Reacts
24:43
ThePrimeTime
Рет қаралды 181 М.
Don't Model the Problem
14:32
David Hart
Рет қаралды 13 М.
End of Epoxy Tables
30:04
Blacktail Studio
Рет қаралды 1,6 МЛН
The Weirdest Language I've Ever Seen (I kinda love it...)
58:11
Theo - t3․gg
Рет қаралды 53 М.
There's A New Browser (That ISN'T Chrome Based)
47:59
Theo - t3․gg
Рет қаралды 48 М.
I screwed up.
7:47
Theo - t3․gg
Рет қаралды 119 М.
`const` was a mistake
31:50
Theo - t3․gg
Рет қаралды 125 М.
Insane Vulnerability In OpenSSH Discovered
1:06:56
ThePrimeTime
Рет қаралды 151 М.
cute mini iphone
0:34
승비니 Seungbini
Рет қаралды 6 МЛН
ОБСЛУЖИЛИ САМЫЙ ГРЯЗНЫЙ ПК
1:00
VA-PC
Рет қаралды 355 М.
Неразрушаемый смартфон
1:00
Status
Рет қаралды 2,3 МЛН
Урна с айфонами!
0:30
По ту сторону Гугла
Рет қаралды 8 МЛН