r/ProgrammerHumor • • Mar 20 '24

Meme areJSDevsActuallySane

Post image
14k Upvotes

554 comments sorted by

View all comments

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.

2

u/Frown1044 Mar 21 '24

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.