JavaScript is notorious for its inconsistencies in a lot of areas. It's also powerful and popular and has a lot going for it.
But can we poke fun at weird things? (whether or not the behavior is a good thing in general)
Let's start with an example:
+'a' resolves to NaN ("Not a Number") because it coerces a string to a number, while the character a cannot be parsed as a number
document.write(+'a');
nan.
Adding NaN to "ba" turns NaN into the string "NaN" due to type…
Latest comments (85)
This took hours of my life, i want them back!
I'd say definitely this:
The weirdest thing for me in JS is the fact that you will only know if something works at runtime because there isn't any compilation.
This brings weird stuff like the undefined state. It is basically telling you that you are using something that doesn't exist. You know it should exist at that point, but because of some mistake somewhere you cannot identify because there is no compilation, you will have to check if that shit is undefined before checking if it is null before checking if it is empty.
Yes, I'm new to JS and I know you can test any variable as if they were Boolean to check for undefined and null, but I think that's not readable enough, so I would rather create a function with another name that checks for that.
Ben thanks for this! Over the years I've loved the quirks of JavaScript. It's a totally different way of coding, and I like it as an option. I'll stick to C# if I want true OO.
My favorite two have always been the guard clause (objNullRef && objNullRef.prop) and the default clause (objNullRef || fallBackObj).
Long live JavaScript!
Love these other comments, you guys are great!
this one left me wondering quite a while why my API was returning null:
I <3 Javascript, but: destroyallsoftware.com/talks/wat :D
Imo the implicit type conversion is a little weird, but even ignoring that, I think the point was the difference in behavior between
'11' + 1, which converts the number to a string, and'11' - 1, which converts the string to a number. This discrepancy makes implicit type casting feel unpredictable and weird, at noon least to meSo many things!
I made posts about my favorites, among which:
JavaScript: Equality insanity, or where x === 1 && x === 2
Antony Garand ・ Jun 15 '18 ・ 5 min read
JavaScript: Async math is hard
Antony Garand ・ Jun 11 '18 ・ 4 min read
You can also question your knowledge of these weird things on this challenge: alf.nu/ReturnTrue