This. In python everything you'd intuitively expect to work will work for the most part, and things that make no sense result in exceptions. JS, on the other hand..
Why does that not make sense? Adding an object to a string implicitly converts the object to a string and then concatenates the two strings, and the default conversion for object is "[object Object]" if .toString() isn't defined.
Next you're going to tell me that 5 + 1.0 should also error because it implicitly promotes an integer to a double.
Edit: so this comment is dragging out all of the butthurt python fanbois. Here's the deal: your pretty little scripting language was written with auxiliary operating system tasks in mind, and was later adopted by statisticians and mathematicians. Of course it has hard typing and extensive error handling.
But JavaScript was originally integrated into HTML. That's frontend. Frontend needs implicit string conversions, because typing str(some_var) over and over again gets real annoying, real fast. "10" == 10 is a bit more arguable, but I suppose it has its use in validating input. Also, when you have a user on your frontend, the last thing you want is everything to come crashing down because of some formatting error in a string that gets shown to the user maybe once in a blue moon. There's probably some performance reasons for the way things are as well, because V8 compiles hot code into machine code - I imagine it's cheaper to just have a toString() defined to return a constant somewhere instead of checking for nullptr and slinging errors around...
In any case, Lua is, objectively, the best scripting language.
Honestly, that's an extremely non-specific and unhelpful representation of an object. I would like it much more if they converted it to JSON (shouldn't be that hard, since they already parse JSON as part of the language).
The second "Object" is actually the class of the object:
"b" + (new Event('hello')) // "b[object Event]"
So there is some logic to it, it's just not the best logic there could be. Would it make more sense to JSON-stringify it? Yes, hella yes. But the problem is, JavaScript actually came first, not JSON, the object to string casting is a core part of the language, and JS is completely backwards-compatible (it kinda has to, a breaking change would potentially break the entire web). So, unfortunately, there is no way to change that now.
It makes sense in that it's a well-defined behavior, but you would never actually want that behavior. You'd be far more likely to do something like that accidentally, but since JS likes to do implicit type conversions you wouldn't necessarily notice that you'd made a mistake. Far better to get a type error and a stack trace so you know you've made a mistake and find the exact line it's caused by.
Because javascript isn't strongly typed, which means if a string sneaks in somewhere, this bug could easily bite you in the ass without raising any errors and being hard to track down.
I think js is more scary on where it implicitly changes one thing to the something else. Not that python doesn't have similarly grave issues, but some of the stuff it does by itself become issues that aren't immediately seen and breakes something else later on.
Python is a little more strict on what you can have interact, eg. string + number is a no go, but in JS, the string might be converted to an number and added to the number, if it's possible.
I think others have answered, but the main thing people dislike in Javascript regarding this is the really aggresive string coercion that Javascript performs. It also happily doesn't error when things go wrong a lot of the time. Want to multiply a string by another string? Well that doesn't error, it just returns NaN.
This allows beautiful lines such as this one which I'll let you try to work out how it works.
Python doesn't allow any of this. Each interaction is explicitly defined.
[1] * 4 = [1, 1, 1, 1]
"a" * 4 = "aaaa"
You also see this in things like list/array addition:
There's also some ridiculous errors to try and debug in rare situation:
let myVariable = null
let x = parseInt(myVariable, 24)
// x => 23
The above is a real interesting one. You are asking Javascript to convert a base 24 number (in string form) into a decimal number. In this case, N or n is a valid character in base 24 and it equals 23. null is transformed into the string "null" and then Javascript will just stop, without an error, when it hits the u which is a letter it doesn't recognise in base 24 (so again, no erroring).
Basically, that's why people dislike it in Javascript.
121
u/[deleted] Aug 26 '20
Wondering though, why do people consider this a good thing in Python but a bad thing in JS?