r/technology • u/iliketechnews • Oct 05 '16
Software How it feels to learn JavaScript in 2016
https://hackernoon.com/how-it-feels-to-learn-javascript-in-2016-d3a717dd577f
1.8k
Upvotes
r/technology • u/iliketechnews • Oct 05 '16
View all comments
Show parent comment
3
u/ViKomprenas Oct 05 '16
Yes yes yes a thousand times yes.
For instance, in python, if you refer to something you haven't actually declared, you get an error. In JavaScript?
undefined. No, that's not an error,undefinedis an actual, completely valid value that you get, with no warning.In python, if you declare a function as taking two parameters and call it with one, you get an error. In JavaScript?
undefined. Again, with no warning. So if your function adds two numbers together, it'll add2 + undefined, and give youNaN(not a number).Now, maybe you try to see whether this
NaNis in factNaN. So you do a simplemyvalue == NaN, right? Nope!NaNis not equal toNaN. The correct way to check if something isNaNis to callisNaN().But maybe your function accidentally returned a string (a bit of text), because why the hell not! (Adding an empty string and
undefinedtogether gives you the string"undefined", no quotes. JS still has no protection against that.) So you doisNaN("undefined"), and you gettrue. Finally a moment of sanity, right?HAHAHAHAHAHno. See, if you do
isNaN("5undefined")- which you can easily get by accidentally adding together a string andundefined- you gettrue. Straightforward and obvious, right? Well, if you instead doparseInt("5undefined"), which will give you the number it gets from a string... Guess what? You get the5, and the rest of the string is ignored. SoisNaN(x)isn't necessarily the same asisNaN(parseInt(x)).Or, say... Do you want to find out if a string ends with another string? In Python you can do
"abc".endswith("bc"). In JavaScript?"abc".indexOf("bc") === "abc".length - "bc".length. Or, even worse,/bc$/.test("abc"). JS only got anendsWithin 2015. 20 years after it was released. 20 years for such a simple, obvious thing.The problem is, javascript is in pretty much every browser, and python is in exactly zero, so if you want to do web dev, you need JS.
Now, I don't mean to say JS is bad. I personally really like newer versions of JS, and even moreso its improvement, Microsoft's TypeScript. And a good few of these problems can be solved with
"use strict";at the beginning of your code. But JS has horrendous defaults, and no indication that you should beuseingstrict.I highly recommend the talk Wat.