The square bracket is not typed, the {} object gets converted into a string representation which is [object Object]. These square brackets then have regex meaning, match anything inside of them. The o matches Mom, there is nothing in the brackets that match Dad.
Despite having a 3 year old account with 150k comment Karma, Reddit has classified me as a 'Low' scoring contributor and that results in my comments being filtered out of my favorite subreddits.
So, I'm removing these poor contributions. I'm sorry if this was a comment that could have been useful for you.
Despite having a 3 year old account with 150k comment Karma, Reddit has classified me as a 'Low' scoring contributor and that results in my comments being filtered out of my favorite subreddits.
So, I'm removing these poor contributions. I'm sorry if this was a comment that could have been useful for you.
parseInt with arity 1 interprets its argument as a string representing a base-ten int and stops parsing when it encounters the first character that doesn't make sense for that. Numbers are doubles/float64s, Number.MIN_VALUE gives you the smallest positive value representable by that which is 5e-324. parseInt stops parsing that with the e.
parseInt with arity 2 takes the base as the second argument, you can do convenient things like parseInt(parseInt, 35) which is 1021464698788.
Because it’s not breaking down and analyzing it. It’s just producing text that looks as similar as possible to what text of someone breaking it down and analyzing it would look like.
The best formulation of this concept ive heard. It's insane that "producing text that looks like code that does what you asked" or "producing text that looks like debugging your code" will do those things.
You forgot "your comment as a haiku which is actually off by one syllable but it's ok because there's this obscure joke in Avatar The Last Airbender where one of the main character does the same thing"
Kinda bizarre to see a Reddit post of a screenshot of a twitter post about my Reddit post got on #1 hot of the same subreddit where I originally posted.
The issue is that en object when cast to string would produce a descriptive string such as "[Object object]" which would only make sense inside a printing / logging / debugging function.
This is the n-th post I see about accidental object to string conversions, while in like 10y of writing a ton of JS with various frameworks it happened by accident around...3 times. This underlines very clearly how humans are in general, they get obsessed and stuck on the most minor things that appear unusable or something at first glance, and then keep beating that dead horse.
I see what you’re saying, and this example is clearly made to highlight an extreme case, but wouldn’t the language be better if it didn’t do weird object conversions like this?
I see the argument a lot that while writing JS you don’t often encounter these language quirks as long as you structure your code well, but I tend to stand behind the idea that it would’ve been better for the language to not allow you to do these things in the first place.
JavaScript is a great language but it has parts of the spec that were really quirky decisions. Unfortunately, things like conversion to [object Object] are indeed specified behavior, not undefined behavior, and are therefore unlikely to ever change. So we just have to live with this stuff now. But as the previous commenter mentioned, in my 7 years of writing JS I’ve been tripped up by zany type conversions only maybe 3 or 4 times. Not a prevalent problem for most JS devs.
It's the same situation for JS and Python. JavaScript was originally intended for simple scripting for webpages, in earlier times when we couldn't imagine just how powerful 21st century websites would become, while Python was intended to be simple. Strict typing was either considered unnecessary or undesired. It was a conscious design choice, and back then the concept of good programming practices and engineering wasn't very mature yet.
Fast forward to today, and strict typing is more important than ever for good code and such. NodeJS and Python are among the world's most popular languages now, however since typing wasn't originally part off the language's design, they've to retrofit and introduce it in a roundabout way which won't break legacy code.
In other words, JS does weird stuff like this because of old design decisions. It's always legacy, legacy, legacy.
At least Microsoft got some of the best language developers in the world for TypeScript, and though it does have a few rough edges, overall they’ve done an astonishing job rationalizing the insanity we started with.
Yes, people shouldn’t make excuses for stupid behaviour like this. But at least if you look at any APIs added to the platform in the last five or ten years, they throw errors like you’d hope a sane language would. And any responsible developer these days will be using typescript and avoid 99% of these issues ahead of time. It’s a pretty good experience if you’re using modern tools, but the legacy baggage is going to be with us forever.
Typescript is great but it gets cumbersome because it really puts an accent on the types more than every other language out there. You have to litter every file with dozens of imports for every type you use, which makes decoupling harder. You end up almost having to write your application code twice, once in type logic and once in actual logic. Once you start doing something with any meaningful degree of complexity the type mess that naturally arises is more silly than C++ templates.
That tile on the floor is actually lava, but only that one, I’ve only lost three guests to it, I don’t know why people keep complaining about it, just step around it, not that hard
Or we could use a contractor that doesn’t just have a lava tile on the floor, or at least puts up a fence so people don’t just fall in
Heh, that's one way of framing to make something appear more important, when I just said it's basically irrelevant to me, I barely noticed it's a case that exists.
Or of the 30 total interesting things to even post about Javascript, 28 or them are unexpected type conversions. So what do you expect exactly from a Javascript post? Sheer boredom? Would that make you happier?
guys please implicit conversions are good no they're useful because they uhh.... you dont have to write them out yourself they just happen and that will save you time, no please just.... its... you dont have to write anything tho its just does that... no you can know when but, no wait guys come back its saves time i swear guys please its helpful please!
Almost every "weird" thing is just a contrived example of type coercion and if you know how it works it makes sense even if you may not like it and prefer stronger types. And it never really comes up once you know JS because you just know what types things expect and sort of mentally do your own type checking.
Type coercion actually makes sense here, just not in every single context.
In JS you can write code like new RegExp(/abcdef/g). The regex inside is an object, not a string. It can get converted to its equivalent string "/abcdefg/g" however.
Likewise other things that implement .toString() can return valid input.
Oh right, let's code the weirdest thing. Like give an object to a regex check and expect something that automatically makes sense instead of you know, coding code that has any real use.
And while I agree with people that the language probably shouldn't even allow a lot of the weird shit we see with JS, that doesn't make it humor. Like ok not a great language design but how is it funny especially when there's an explanation in the OP. I write Rust, Go, Java, and JS and I've never in 20yrs+ of programming needed to write such weird obscure shit. At this point JS posts like this should be considered low effort and removed.
Yes, JavaScript cannot seriously be run with a linter. Pick your favorite: jshint, jslint, eslint, or entirely over to TypeScript. It's a little like the hundreds of tricky C puzzles solved by adding parenthesis in the correct place.
And yet, I still need separate build tools and package managers and deployment managers and cloud managers to print "hello, world".
I have for years now said that the difference between Java and Javascript is about the same as the difference between an ordinary bike and a one wheel bike.
Javascript is slightly more lightweight. It has no other technical advantage.
No offense to the Javascript developers: it makes what you do much more impressive.
I am also humble enough to admit that I am fairly sure I would not have been able to make a better language in three weeks myself.
But no way it is allowed in any of my projects now that TypeScript exists.
I'll never understand how a literal deathtrap turned into code became so widespread, especially in education. It is like handing gunpowder and shells to new recruits right off the bat...
I get it... JavaScript dumb... But who would EVER try to do this? How could this possibly cause an issue for anyone who has the vaguest sense of what they're doing?
When you test your regex and it doesn't work... That's how you know it's broken and you need to debug it.
It's crazy that people don't shit all over other languages that have generic errors like "seg fault".. sometimes people do things so silly that there's no explicit error message. That's not a fault of the language.
When you test your regex and it doesn't work... That's how you know it's broken and you need to debug it.
Dude, I hate "JS bad because I did silly thing" criticisms as much as the next person, but "just fix your regex when you finally notice it's randomly failing for no reason" is not a good defense of the language.
sometimes people do things so silly that there's no explicit error message. That's not a fault of the language.
That's absolutely a fault of the language. What else would it be?
The middle step is wrong. {} is directly converted to a string with {}.toString(), which indeed results in [object Object]. Writing new RegExp([object Object]) is incorrect.
as always with these posts, the secret is implicit type coercion. {} converted to a string is "[object Object]". Regex expects a string. JS makes sure it gets one by feeding the console log representation of whatever garbage non-string data you gave it.
Once again, yet another JS truth table, crying about a stupid scenario clearly specified, serving as platform for juniors trying to look like they know something.
Because the designer had to shove something out the door in a hurry.
The language was relegated to minor tweak scripts for a decade until Douglas Crockford published "Javascript: The Good Parts" and made a language that was good enough to write client side code. Since then, it has made simple evolvutions for a decade to become the ECMA Script we use today.
There is an insane amount of backward compatibility, so a script saying "a == b" can rely on arcane conversion rules, though every build environment will scream about it.
I think it's worth keeping in mind if just wanted a regex in JS you'd use literal notation:
const re = /ab+c/i;
And using new Regexp() is something you'd use for dynamic expressions. Could it be better, obviously, but you have to try to actually replicate the result in the post. There's plenty of worse things to worry about, even with Regular expressions, if you don't pass an argument to .test() it'll search for "undefined" so RegExp({}).test() is true and RegExp("mom").test() is false
Yes, we are sane, but it's a different kind of sane, unrecognizable as such by anyone with normal thought processes.
And that is itself exactly the sort of thinking that makes the way JS behaves comprehensible to us.
Go figure.
Edit: You should try native Clipper. Not only is everything a 'var', it has both lexical and dynamic scoping. You can do anything in the language, and almost all of it is wrong.
Remember people, when Javascript expect a string, it will convert whatever in front of it to a string. Every object inherit tostring (its a property of object) , therefore every object behave as a string. Its a consistent behavior
If you learn how the prototype system of JS work, it is really intuitive
See? JS makes sense. Even though it's typeless, just assume it'll convert to some relevant type when something doesn't make sense and there's no error. This is essentially (quite literally) the to string method, so if other languages function fine with such then JS can too
Honestly the fact that anyone defends using a language employing this much secret under-the-hood magic in anything but a prototyping role terrifies me.
Like how tf is anyone supposed to find a bug caused by this interaction in a program bigger than 10 lines?
One thing you could do is just... you know... avoid implicit conversions unless you know what you're doing. I avoid == like the plague and I make sure to know what's truthy and falsely. Oh, and use typescript.
I feel like a lot of posts here are posted by people who may be coders but don't understand like syntax and how different languages interperate things. I am by no means against JS and understand it's weaknesses but every language has its place.
I don't get why JavaScript wasn't changed years ago to not do all this overloading. The RegExp() function should only accept strings... Why would someone want to input something other than a string into the RegExp() function? If you want to do regular expressions on strings that reside inside some other data type (e.g. array), then make a new function that makes use RegExp() and give this new function a proper name to reflect what it does...
When is this overloading shit helpful? There's no way the pros benefit the cons. It just creates problems and solves nothing.
The actual issue is that there is NOT an overload that takes an object. The object is coerced into a string BECAUSE the method can't handle accepting objects.
3.1k
u/Ambitious-Finance-83 Mar 20 '24
can't wait for this to come up in an interview...
1.2k
u/Spot_the_fox Mar 20 '24
Huh, neat
829
u/Kashrul Mar 20 '24 edited Mar 20 '24
I know nothing about js, maybe that's why I can't see square brackets in initial question?
184
u/PM_ME_YOUR__INIT__ Mar 20 '24
Screenshot of a repost of a reddit post from yesterday? https://www.reddit.com/r/ProgrammerHumor/s/pJRAv69CZ5
47
u/SoRaang Mar 20 '24
There's nothing
console.log()can't handle70
u/FrumpyPhoenix Mar 20 '24
Thanks, I hate it
19
u/NatoBoram Mar 20 '24
Can't unhear Fireship's pronunciation of
[object Object]25
u/R4XD3G Mar 20 '24
Well, the issue is they forgot
JSON.stringify()13
u/[deleted] Mar 20 '24
Eh, you can do something silly with any language if you try
In c++ you can say
++++--++xbut you can't say
x++++Not sure why you'd ever want to, but if we're doing RegExp({}), why not code something else stupid
6
u/pceimpulsive Mar 21 '24
Js is cracked mate... Wtf
89
u/Leonhart93 Mar 20 '24
This is the n-th post I see about accidental object to string conversions, while in like 10y of writing a ton of JS with various frameworks it happened by accident around...3 times. This underlines very clearly how humans are in general, they get obsessed and stuck on the most minor things that appear unusable or something at first glance, and then keep beating that dead horse.
5
u/KSP_HarvesteR Mar 21 '24
The explanation just made it more horrifying
13
u/Rest-That Mar 20 '24
I hate that this makes sense
9
u/6tPTrxYAHwnH9KDv Mar 20 '24
I see parts like this and think to myself "how neat!" And then I remember that this is not fucking normal.
3
u/Lazarus_Octern Mar 20 '24
I like that there is always some actual reason why jank like this exists but i dont like that it exists at all....
3
u/[deleted] Mar 20 '24
Every time I read (object Object) i read it in the voice of driods from star wars.
3
u/FromZeroToLegend Mar 20 '24
People complain about Js with examples that you’d never use in real life
3
u/CringyDabBoi6969 Mar 21 '24
guys please implicit conversions are good no they're useful because they uhh.... you dont have to write them out yourself they just happen and that will save you time, no please just.... its... you dont have to write anything tho its just does that... no you can know when but, no wait guys come back its saves time i swear guys please its helpful please!
5
u/Slusny_Cizinec Mar 20 '24
As always: https://www.destroyallsoftware.com/talks/wat
5
u/wishper77 Mar 21 '24
In Italian 'mom'='mamma' and 'dad'='papà' Both will lead to false
So Js is not broken. English is
/s
7
u/TracerBulletX Mar 20 '24
Almost every "weird" thing is just a contrived example of type coercion and if you know how it works it makes sense even if you may not like it and prefer stronger types. And it never really comes up once you know JS because you just know what types things expect and sort of mentally do your own type checking.
8
u/CirnoIzumi Mar 20 '24
what a robust language, lets build servers and apps in it
13
u/DeathUriel Mar 20 '24
Oh right, let's code the weirdest thing. Like give an object to a regex check and expect something that automatically makes sense instead of you know, coding code that has any real use.
Sorry, I meant... JS BAD, BOOOO!
4
u/AdvanceAdvance Mar 20 '24
Sigh, boring.
Yes, JavaScript cannot seriously be run with a linter. Pick your favorite: jshint, jslint, eslint, or entirely over to TypeScript. It's a little like the hundreds of tricky C puzzles solved by adding parenthesis in the correct place.
And yet, I still need separate build tools and package managers and deployment managers and cloud managers to print "hello, world".
2
u/wutzebaer Mar 20 '24
That's why you use typescript
2
u/[deleted] Mar 20 '24
wat
2
u/sebbdk Mar 20 '24
Why would you do that?
2
u/Silly_Guidance_8871 Mar 20 '24
I don't like being reminded that PHP is the more sane of my 2 day-driver languages, thank you very much.
2
u/eitland Mar 21 '24
I have for years now said that the difference between Java and Javascript is about the same as the difference between an ordinary bike and a one wheel bike.
Javascript is slightly more lightweight. It has no other technical advantage.
No offense to the Javascript developers: it makes what you do much more impressive.
I am also humble enough to admit that I am fairly sure I would not have been able to make a better language in three weeks myself.
But no way it is allowed in any of my projects now that TypeScript exists.
2
u/Honeybadger2198 Mar 21 '24
Browsers are fault-tolerant to a fault.
2
u/blindada Mar 21 '24
I'll never understand how a literal deathtrap turned into code became so widespread, especially in education. It is like handing gunpowder and shells to new recruits right off the bat...
2
u/AreaExact7824 Mar 21 '24
Wtf. Just keep away with js
2
u/LegenDrags Mar 21 '24
I fucking love javascript
2
u/Bagel42 Mar 21 '24
This is why typescript was invented
2
u/Modo44 Mar 21 '24
Every time I see JS-related shenanigans, I am more in awe of any of my browser-based services working, like, at all.
2
u/masterKick440 Mar 21 '24
I'm not sure that explanation made it any better.
2
u/Ri0ee Mar 21 '24
99% of JS memes are just "garbage in - garbage out" code and even explaining it does not change the fact
2
u/Lopsided_Afternoon41 Mar 21 '24
I feel as though I'd have picked up programming a lot quicker, had I not started with JavaScript.
2
u/Penguinmanereikel Mar 21 '24
Makes sense, but I still want to kill it.
2
u/ComposerTraining Mar 21 '24
Mom's an object...
6
u/subject_deleted Mar 20 '24
I get it... JavaScript dumb... But who would EVER try to do this? How could this possibly cause an issue for anyone who has the vaguest sense of what they're doing?
4
u/wildjokers Mar 20 '24
But there are no square brackets in those lines of code. So his point 2 doesn't make sense.
2
u/[deleted] Mar 20 '24
No were not :3
2
u/TheMoonMoth Mar 20 '24
Hey I'm a JS Dev. The answer is no.
2
u/gangze_ Mar 20 '24
No, and I question my carreer path every day :)
2
u/kingottacYT Mar 20 '24
am js dev, can confirm i am not sane
2
u/GoodEfficiency8472 Mar 20 '24
I know nothing about JavaScript, perhaps that's why I can't see square brackets in the initial question?
2
u/anonymous_sentinelae Mar 20 '24
Once again, yet another JS truth table, crying about a stupid scenario clearly specified, serving as platform for juniors trying to look like they know something.
2
u/No-Con-2790 Mar 20 '24
That explains the how but not the why! Why would anyone think this is a good idea.
2
u/1Dr490n Mar 20 '24
I’m not sure what I hate more about this code: the result or that it’s called RegExp instead of Regex
2
u/alterNERDtive Mar 20 '24
Whenever you think JS can’t possibly be even worse than you already think, something like this comes along.
1
u/neriad200 Mar 20 '24
a small part of my soul has surely died.. this shit-tier language can't really be what's holding up most web pages today, can it?
1
u/Specialist-Tiger-467 Mar 20 '24
No we are not. And that's why we are dangerous.
1
u/SarcasmsDefault Mar 20 '24
This court has found you are… not the ‘dad’, but you are the ‘father’
1
u/ilmk9396 Mar 20 '24
this is hilarious
1
u/emetcalf Mar 20 '24
"working as intended"
1
u/Acceptable-Tomato392 Mar 20 '24
As a JS dev, I can certify that we are most definitely not.
1
u/thatmaynardguy Mar 20 '24
Yes... and no... and nan....
1
u/fzzzzzzzzzzd Mar 20 '24
Sane devs know about well documented javascript features, and that javascript has quirks. :)
1
u/an_agreeing_dothraki Mar 20 '24
no. you can't trust them. they're all insane. The console told me so, but it was also in on it.
That's why I do all my debugging with alert() commands.
Means the government can't getcha
1
u/breischl Mar 20 '24
Next time I get annoyed about having to manually call
.toString()in Java, I'm going to remember this.1
u/Semper_5olus Mar 20 '24
I gotta stop telling potential employers I can use JavaScript.
JavaScript makes no sense.
1
u/[deleted] Mar 20 '24 edited Mar 20 '24
Yes, we are sane, but it's a different kind of sane, unrecognizable as such by anyone with normal thought processes.
And that is itself exactly the sort of thinking that makes the way JS behaves comprehensible to us.
Go figure.
Edit: You should try native Clipper. Not only is everything a 'var', it has both lexical and dynamic scoping. You can do anything in the language, and almost all of it is wrong.
1
u/Tc14Hd Mar 20 '24
Here is my own version:
Which outputs:
1
u/exmachinalibertas Mar 20 '24
javascript is such a fucking trainwreck I don't understand how it remains popular when other things like burning yourself alive exist as alternatives
1
u/Cody6781 Mar 20 '24
Omg guys why I use stuff in ways it was never meant to be used funny stuff happens omg guys look look
1
u/MajorTechnology8827 Mar 20 '24
Remember people, when Javascript expect a string, it will convert whatever in front of it to a string. Every object inherit tostring (its a property of object) , therefore every object behave as a string. Its a consistent behavior
If you learn how the prototype system of JS work, it is really intuitive
1
u/SaliferousStudios Mar 20 '24
oh javascript.
1
u/[deleted] Mar 20 '24
It’s fine as long as you understand the quirks of JS.
1
u/wonkey_monkey Mar 20 '24
I feel like that maybe kind of explains it but it doesn't really explain it...
1
u/Naliwe Mar 20 '24
Huh, perfect, can you bundle it in a npm package ? I need a 'isMom' function
1
u/BruceJi Mar 21 '24
I feel like JS is legacy code itself
1
u/Substantial-Mango499 Mar 21 '24
i need some help, where's the [] square brackets?
1
u/lostBoyzLeader Mar 21 '24
That’s fucking hilarious
1
u/1984isAMidlifeCrisis Mar 21 '24
If you debug it, you can see it.
1
u/Erizo69 Mar 21 '24
holy shit
1
u/DmC8pR2kZLzdCQZu3v Mar 21 '24
What a nightmare :(
1
u/Hidesuru Mar 21 '24
Any language that isn't strongly typed is weird to me lol.
1
u/RevealingPanda Mar 21 '24
Oh, I thought it matches with object because women are objects.
1
u/bikemandan Mar 21 '24
{} turns into a string?? wtf
1
u/[deleted] Mar 21 '24
Every WTFJS post is a lesson in static typing
1
u/TuVeuxBaiser Mar 21 '24
Wait, so every time {} is used it brings in what would be an object except its always just a string that says object?
I mean, this seems insane... what am I missing? What is the use case for a string that says object Object that is in no way an object?
1
u/revolutionPanda Mar 21 '24
I see stuff like this all the time and people flex about knowing the answer. But wouldn't nothing like this ever come up in a real situation?
1
u/J4m3s__W4tt Mar 21 '24
It's almost like JS was designed to quickly hack together 100 lines basic interactive functions for websites and not for running actual applications.
1
u/vimescarrot Mar 21 '24
As someone who's tried to learn programming, this looks exactly like anything I tried to learn!
1
u/LustyHasturSejanus Mar 21 '24
Python devs acting smug when they get a runtime type error.
1
u/P0pu1arBr0ws3r Mar 21 '24
See? JS makes sense. Even though it's typeless, just assume it'll convert to some relevant type when something doesn't make sense and there's no error. This is essentially (quite literally) the to string method, so if other languages function fine with such then JS can too
1
u/Renard_Fou Mar 21 '24
Square brackets ? I dont see any...
1
u/[deleted] Mar 21 '24
You see what happens larry, this is what happens when you automatize type conversion !
1
u/wise_chain_124737282 Mar 21 '24
Js so bad its meme tops ProgrammersHumor thread daily ☠️
1
u/whackamattus Mar 21 '24
Lol no it doesn't need a default just throw a type error
1
u/GaraBlacktail Mar 21 '24
Am I blind?
I see no square brackets, so how does that work??
1
u/error_98 Mar 21 '24
Honestly the fact that anyone defends using a language employing this much secret under-the-hood magic in anything but a prototyping role terrifies me.
Like how tf is anyone supposed to find a bug caused by this interaction in a program bigger than 10 lines?
1
u/Dragonfire555 Mar 21 '24
One thing you could do is just... you know... avoid implicit conversions unless you know what you're doing. I avoid == like the plague and I make sure to know what's truthy and falsely. Oh, and use typescript.
1
u/x39- Mar 21 '24
Javascript is cursed and no sane person should ever use it
Which is why I am still baffled that there are pleb out there using js for backend development
1
u/TenYearsOfLurking Mar 21 '24
JS is smart as
{}looks like... well, a mom1
u/CaitaXD Mar 21 '24
mum & pops > mom & dad
1
u/molx730 Mar 21 '24
I feel like a lot of posts here are posted by people who may be coders but don't understand like syntax and how different languages interperate things. I am by no means against JS and understand it's weaknesses but every language has its place.
1
u/Linuxfan-270 Mar 23 '24
Even JS hates JS:
```
console.log(new RegExp({}).test('JavascriptIsBad'));
console.log(new RegExp({}).test('PlainHTMLIsBad'));
```
1
u/VergilSpardaa Mar 20 '24
{} = mom = women = object?
1
u/OnceMoreAndAgain Mar 20 '24 edited Mar 20 '24
I don't get why JavaScript wasn't changed years ago to not do all this overloading. The RegExp() function should only accept strings... Why would someone want to input something other than a string into the RegExp() function? If you want to do regular expressions on strings that reside inside some other data type (e.g. array), then make a new function that makes use RegExp() and give this new function a proper name to reflect what it does...
When is this overloading shit helpful? There's no way the pros benefit the cons. It just creates problems and solves nothing.
1
u/TDR-Java Mar 21 '24
And that’s why this language should not be taken seriously. This is just bullshit behavior