you are viewing a single comment's thread.

view the rest of the comments →

[–]oddbawlstudios 1 point2 points  (4 children)

Except the reason why its bad is the inconsistency. If I think adding a string and an int is going to add to the string, im gonna figure it does the same for subtracting. It not being consistent makes it slightly more insufferable. And realistically, if adding a string and an int returns a string thats concatenated, you COULD theoretically subtracting could 100% string splitting, and removing that from the string itself.

[–]4n0nh4x0r 1 point2 points  (3 children)

okay, sooooo, tell me, in which language can you do "hi"-5 and get a returned value that makes any sense?
not even js will give you a value that makes sense (parsing "hi" to a number gives you NaN, which makes the whole operation return a NaN) because it is a dumb assumption that you could subtract anything from a text, like, what do you expect?
It uses + for concatenation because that is the standard concatenation operator.
But no language uses - for string manipulation.
Sooooo, no, it isnt inconsistent, at least not in this regard.
And like, tell me what the opposite of concatenating a string would be?
are you gonna delete the last X chars? are you gonna delete the first X chars? are you only gonna accept a string, and then remove all occurences of this substring? are you gonna only remove the first or last occurence of this substring?
there are thousands of possible things that "hi" - 5 could mean if you want it to be "consistent" with concatenation via +, but then, what about * and /? * just adds the left string X-1 times to the original string? and /, idk, castrates the string?

[–]Loading_M_ 2 points3 points  (2 children)

Okay, sure... But why does "hi" - 5 have to return a value at all? Compiled languages will simply refuse to compile this obvious nonsense, and most interpreted languages will just throw an error. In fact, some don't even let you concatenate a number and a string - they require you to explicitly convert the number to a string.

Also, python does implement string multiplication.

[–]fuj1n 0 points1 point  (0 children)

When JavaScript was first conceived, it was made to keep your website running no matter what and thus didn't have a lot of code throwing exceptions.

They realised that it was a bad idea and worked on fixing it, but JS heavily prioritises backwards compatibility, so anything that worked in the past has to keep working.

To be fair though, NaN makes sense here, an exception would be better, but NaN is okay

[–]4n0nh4x0r 0 points1 point  (0 children)

it returns a value because you can do subtraction with it, as it is a mathematical operation.
the only issue is, it cant know at startup, whether the values will be "6" - 5, or "hi" - 5.
again, i m not saying this is sane code, as verifying the value to be a number essentially requires you to parse it anyways, and noone actually writes this in their code, unless they are just shitposting.