It’s not nonsensical, implicit type coercion is a feature of JavaScript, it’s perfectly logical and predictable.
JavaScript is a filthy beast, it’s not the right tool for every job, but it’s not nonsensical.
When you follow a string with a +, it concatenates it with the next value (converted to string if needed). This makes sense, and it’s a very standard convention in most languages.
Applying arithmetic to a string would be nonsensical, which they don’t do.
Sure. And you’re entitled to yours. But words have meaning and this isn’t MY OPINION, it’s objective reality. It follows strict rules for predictable output, it is not nonsensical.
You’re entitled to think it’s nonsense, and you’d be wrong. You don’t have to like implicit type coercion, but it’s popular and in many languages for good reason…
Language
Implicit Coercion Example
JavaScript
'5' - 1 → 4
PHP
'5' + 1 → 6
Perl
'5' + 1 → 6
Bash
$(( '5' + 1 )) → 6
Lua
"5" + 1 → 6
R
"5" + 1 → 6
MATLAB
'5' + 1 → 54 (ASCII math)
SQL (MySQL)
'5' + 1 → 6
Visual Basic
'5' + 1 → 6
TypeScript
'5' - 1 → 4
Tcl
"5" + 1 → 6
Awk
'5' + 1 → 6
PowerShell
'5' + 1 → 6
ColdFusion
'5' + 1 → 6
VBScript
'5' + 1 → 6
ActionScript
'5' - 1 → 4
Objective-J
'5' - 1 → 4
Excel Formula
"5" + 1 → 6
PostScript
(5) 1add → 6
I think JavaScript is filthy, I’m at home with C#, but I understand and don’t fear ITC.
In https://lemm.ee/comment/20947041 they claimed “implicit type coercion” and showed many examples; they did NOT claim “string concatenation”.
However, that was in reply to https://lemmy.world/comment/17473361 which was talking about “implicit conversion to string” which is a specific type of “implicit type coercion”; NONE of the examples given involved a conversion to string.
The NaN isn’t an thrown. It’s just silently put into the result. And in this case it’s completely unintelligible. Why would an operation between two strings result in a number?
"Hello" - "world" is an obvious programmer mistake. The interpreter knows that this is not something anyone will ever do on purpose, so it should not silently handle it.
The main problem here is downward coercion. Coercion should only go towards the more permissive type, never towards the more restrictive type.
Coercing a number to a string makes sense, because each number has a representation as a string, so "hello" + 1 makes intuitive sense.
Coercing a string to a number makes no sense, because not every string has a representation as a number (in fact, most strings don’t). "hello" - 1 makes no sense at all. So converting a string to a number should be done by an explicit cast or a conversion function. Using - with a string should always result in a thrown error/exception.
The interpreter knows that this is not something anyone will ever do on purpose, so it should not silently handle it.
You basically defied the whole NaN thing. I may even agree that it should always throw an error instead, but… Found a good explanation by someone:
NaN is the number which results from math operations which make no sense
And the above example fits that.
"hello" - 1 makes no sense at all.
Yeah but actually there can be many interpretations of what someone would mean by that. Increase the bytecode of the last symbol, or search for “1” and wipe it from string. The important thing is that it’s not obvious what a person who wrote that wants really, without additional input.
Anyway, your original suggestion was about discrepancy between + and - functionality. I only pointed out that it’s natural when dealing with various data types.
Maybe it is one of the reasons why some languages use . instead of + for strings.
Yeah, and almost all languages I know then would throw an exception when you try to use - with a string, and if they offer multiple operators that take a string and a number, they always only perform string operations with that and never cast to a number type to do math operations with it.
(e.g. some languages have + for string concatenation and * to add the same string X time together, so e.g. "ab" * 2 => "abab". It’s a terrible idea to have + perform a string operation and - performs a math operation.)
There is operator overloading happening - the + operator has a different meaning depending on the types involved. Your issue however seems to be with the type coercion, not the operator overloading.
It should not happen no matter why it does happen under the hood.
If you don’t want it to happen either use a different language, or ensure you don’t run into this case (e.g. by using Typescript). It’s an unfortunate fact that this does happen, and it will never be removed due to backwards compatibility.
There is operator overloading happening - the + operator has a different meaning depending on the types involved. Your issue however seems to be with the type coercion, not the operator overloading.
For string + string and number + number there is operator overloading, that’s correct. For string + number there is not, there’s only type coercion. It becomes string + string(number). All of that is fine. Other languages do that as well.
What’s not fine is that JS also looks the other way on the type coercion tree: There’s no string - string overloading, so it goes down the type coercion tree, looking for any - operation that it can cast to and it ends up with number(string) - number(string), which makes no sense at all.
If you don’t want it to happen either use a different language, or ensure you don’t run into this case (e.g. by using Typescript). It’s an unfortunate fact that this does happen, and it will never be removed due to backwards compatibility.
It’s not the point of the discussion that there are other languages that are better. This here is about complaining about bad language design, and no matter how you turn this, this is not a matter of taste or anything, this is just bad language design.
You are obviously right that this crap will stay in JS forever. That doesn’t make it good design.
BS. A language shouldn’t have operators that allow non sensical operations like string concatenation when one operand is not a string.
It’s not nonsensical, implicit type coercion is a feature of JavaScript, it’s perfectly logical and predictable.
JavaScript is a filthy beast, it’s not the right tool for every job, but it’s not nonsensical.
When you follow a string with a
+
, it concatenates it with the next value (converted to string if needed). This makes sense, and it’s a very standard convention in most languages.Applying arithmetic to a string would be nonsensical, which they don’t do.
You are entitled to your opinion. implicit conversion to string is not a feature in most languages for good reasons.
Sure. And you’re entitled to yours. But words have meaning and this isn’t MY OPINION, it’s objective reality. It follows strict rules for predictable output, it is not nonsensical.
You’re entitled to think it’s nonsense, and you’d be wrong. You don’t have to like implicit type coercion, but it’s popular and in many languages for good reason…
'5' - 1 → 4
'5' + 1 → 6
'5' + 1 → 6
$(( '5' + 1 )) → 6
"5" + 1 → 6
"5" + 1 → 6
'5' + 1 → 54
(ASCII math)'5' + 1 → 6
'5' + 1 → 6
'5' - 1 → 4
"5" + 1 → 6
'5' + 1 → 6
'5' + 1 → 6
'5' + 1 → 6
'5' + 1 → 6
'5' - 1 → 4
'5' - 1 → 4
"5" + 1 → 6
(5) 1 add → 6
I think JavaScript is filthy, I’m at home with C#, but I understand and don’t fear ITC.
Also, you contradicted yourself just then and there. Not a single of your examples does string concatenation for these types. It’s only JS
So, I think probably everyone in the thread is “correct”, but you are actually talking past one another.
I think the JS behavior is a bad design choice, but it is well documented and consistent across implementations.
Read the thread again, it seems you slipped somewhere. This was all about the claim that implicit conversion to string somehow could make sense.
C# is filthy. But it explains where you got your warped idea of righteousness.
Especially that + and - act differently. If + does string concattenation, - should also do some string action or throw an error in this situation.
Like what kind of string action?
“Hello” + " world" is what everyone can understand. Switch with “-” and it becomes pointless.
this the “or throw an error”
If you try what I wrote it will throw a NaN. I was asking about the first part of the proposal.
The NaN isn’t an thrown. It’s just silently put into the result. And in this case it’s completely unintelligible. Why would an operation between two strings result in a number?
"Hello" - "world"
is an obvious programmer mistake. The interpreter knows that this is not something anyone will ever do on purpose, so it should not silently handle it.The main problem here is downward coercion. Coercion should only go towards the more permissive type, never towards the more restrictive type.
Coercing a number to a string makes sense, because each number has a representation as a string, so
"hello" + 1
makes intuitive sense.Coercing a string to a number makes no sense, because not every string has a representation as a number (in fact, most strings don’t).
"hello" - 1
makes no sense at all. So converting a string to a number should be done by an explicit cast or a conversion function. Using-
with a string should always result in a thrown error/exception.You basically defied the whole NaN thing. I may even agree that it should always throw an error instead, but… Found a good explanation by someone:
And the above example fits that.
Yeah but actually there can be many interpretations of what someone would mean by that. Increase the bytecode of the last symbol, or search for “1” and wipe it from string. The important thing is that it’s not obvious what a person who wrote that wants really, without additional input.
Anyway, your original suggestion was about discrepancy between + and - functionality. I only pointed out that it’s natural when dealing with various data types.
Maybe it is one of the reasons why some languages use . instead of + for strings.
That’s the case in many languages, pretty much in all that don’t have a separate string concatenation operator.
Yeah, and almost all languages I know then would throw an exception when you try to use
-
with a string, and if they offer multiple operators that take a string and a number, they always only perform string operations with that and never cast to a number type to do math operations with it.(e.g. some languages have
+
for string concatenation and*
to add the same string X time together, so e.g."ab" * 2 => "abab"
. It’s a terrible idea to have+
perform a string operation and-
performs a math operation.)Sure, but then your issue is with type coercion, not operator overloading.
Because there’s in fact no operator overloading happening, true, but that’s mostly an under-the-hood topic.
It should not happen no matter why it does happen under the hood.
Operator overloading for
string - string
is wrong and type coercion to implicitly cast this toint(string) - int(string)
is just as wrong.There is operator overloading happening - the
+
operator has a different meaning depending on the types involved. Your issue however seems to be with the type coercion, not the operator overloading.If you don’t want it to happen either use a different language, or ensure you don’t run into this case (e.g. by using Typescript). It’s an unfortunate fact that this does happen, and it will never be removed due to backwards compatibility.
For
string + string
andnumber + number
there is operator overloading, that’s correct. Forstring + number
there is not, there’s only type coercion. It becomesstring + string(number)
. All of that is fine. Other languages do that as well.What’s not fine is that JS also looks the other way on the type coercion tree: There’s no
string - string
overloading, so it goes down the type coercion tree, looking for any-
operation that it can cast to and it ends up withnumber(string) - number(string)
, which makes no sense at all.It’s not the point of the discussion that there are other languages that are better. This here is about complaining about bad language design, and no matter how you turn this, this is not a matter of taste or anything, this is just bad language design.
You are obviously right that this crap will stay in JS forever. That doesn’t make it good design.