DEV Community

Discussion on: What is the oddest JavaScript behavior?

Collapse
 
peiche profile image
Paul
'11' + 1 // 111
'11' - 1 // 10
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ahkohd profile image
Victor Aremu

😂😂

Collapse
 
leob profile image
leob • Edited

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 ...)

Collapse
 
jacobmgevans profile image
Jacob Evans

JS coercion at its best and worst in two lines lmao!

Collapse
 
xanozoid profile image
XANOZOID • Edited

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.

Collapse
 
alephnaught2tog profile image
Max Cerrina

Absolutely, and not even in a very roundabout way, you're pretty spot on :)