3 ms·
The day JS stops making ’1’ + 2 a string and throw an error is the day I will stop pointing it out. Yes lots of experienced devs use JS. Some of them come from
by IgorPartola 8y ago
The day JS stops making ’1’ + 2 a string and throw an error is the day I will stop pointing it out. Yes lots of experienced devs use JS. Some of them come from other languages with lots of experience. But at the same time, JS language feature development is slow and awkward. This article is a good example of that. I know of no other popular language where accessing the global object is such a mess. And we will wind up with globalThis as some built in language feature which makes no goddamn sense. Read that variable name out loud a few times. Even reading the article you start bumping into “this globalThis variable is this and that”. I swear keeping window would have been preferable. Or calling it global. But instead we have a language spec that allows this this to be different from that this and this global this is not the same as this globalThis and that globalThis.
I point this out because it is important to contextualize why JS is in the state it is in. The powers that be seem less interested in fixing the issues of the languages and more interested in plastering over them. A language with this many polyfills is broken. I use JS almost every day at work, same as Python. 100% prefer Python.
- crooked-v 8y ago> I swear keeping window would have been preferable. Or calling it global. The tc39 proposal for globalThis mentions that `global` was preferred, but it broke major websites that were for whatever reason relying on setting their own values for `window.global`.
- jraph 8y ago> The day JS stops making ’1’ + 2 a string and throw an error Please, no. I'm bitten by this in Python every other day, with its otherwise weak typing. It feels like you can do many horrible things with types in Python, but no, you shall explicitly cast those numbers to strings. It is being more pedantic than Java by doing this. This never caught a real error in my code but it did make it needlessly fail at runtime because I did not put "str()". I know that the newer Python 3.x versions provide a nice bash-like way to embed expressions in strings and would let me avoid the problem entirely, but this would prevent my code to run in many places (and "Hello, I'm {}".format(age) is painful, hence the new feature by the way). Thank you for trying to save my back from unwanted implicit type casts, but provide me with static typing instead if you want to do so, and fail at compile time. If you want '1' + 2 to fail in JavaScript (You might want to, especially when keys of objects are coerced to strings in Javascript, which is actually crappy, and when you are constantly fetching numbers as strings from the web page), use TypeScript. It's amazing and provides a far better way of handling silly type errors. edit: and by the way, C++ is a bit surprising in this respect. "Hello" + 2 is equal to... "llo".
- anoncake 8y ago> Please, no. I'm bitten by this in Python every other day, If you don't know whether your variables contain strings or numbers you literally don't know what your are doing. > with its otherwise weak typing. Python is strongly typed. > It is being more pedantic than Java by doing this. That just means that Java is broken in that respect. > Thank you for trying to save my back from unwanted implicit type casts Making '"1" + 2 = "1twee"' work does not just require a type cast. You actually have to choose and generate one of the string representations of II. > This never caught a real error Yes, it did. You tried to concatenate a string with a number. That cannot be done. > Thank you for trying to save my back from unwanted implicit type casts, but provide me with static typing instead if you want to do so, and fail at compile time. Python is too dynamic for that to work. However Mypy works well enough to be useful.
- shakna 8y ago> edit: and by the way, C++ is a bit surprising in this respect. "Hello" + 2 is equal to... "llo". Only surprising if you think "Hello" is an object of a string type. If however you think of it as a pointer, then moving the pointer is entirely unsurprising, especially as a pointer "is" an integer.
- redwall_hp 8y agoThat's because you're adding two to a pointer, which goes to the zeroth element in an array of integers. Adding two to the pointer is incrementing the memory address, so your pointer is now "l" instead of "H."
- colejohnson66 8y ago> The day JS stops making ’1’ + 2 a string... Don’t C++ and C# do the same thing? Adding something to a string coerces the second thing to a string.