'11' + 1 // 111
'11' - 1 // 10
Question: should we (as JS coders) assume that people know this, and not hesitate to write this sort of code? or should we refrain from this and use explicit type conversion ...
(well the answer is probably that if it is 100% clear that the data type of the first operand is always string and the second operand is always number then this would be okay - if in obscure corner cases it can sometimes suddenly be different then good luck debugging/troubleshooting ...)
JS coercion at its best and worst in two lines lmao!
I love this. It just made me think about something pretty interesting... If you think of this in terms of what it actually could mean for a mathematical function, then the string appendage (regarding integers) could be thought of as its own math operation too - in a very round about way.
Absolutely, and not even in a very roundabout way, you're pretty spot on :)
'11' + 1 is concatenating 2 strings. Don't see anything weird here
'11' + 1
Does JS support operator overloading?
I think yes
It does not
A string and a number to be precise.
Imo the implicit type conversion is a little weird, but even ignoring that, I think the point was the difference in behavior between '11' + 1, which converts the number to a string, and '11' - 1, which converts the string to a number. This discrepancy makes implicit type casting feel unpredictable and weird, at noon least to me
'11' - 1
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.