Gotcha with a catchy title. Punchline: it's all weird. Case closed.
screenshots float around the web to make the case.
222 + 22 // returns 244 "222" + "22" // returns "22222" "222" + 22 // returns "22222"
The addition operator concatenates numeric strings, as well as numeric strings with numbers. This behavior is consistent with the addition of non-numeric character strings:
"ab" + "cd" // returns "abcd"
One may assume then that the subtraction operator would deconcatenate numeric strings and numbers, thus returning "2" in the following case:
"222" - 22 // returns 200
Weird, right? Not exactly, it's pretty logical upon further investigation. If subtraction behaved like addition, you wouldn't be able to make sense of this:
"222" - 11 // returns 211
Sense is made of it though by returning 211. There's no way instead for deconcatenation to make sense of it, just like there's no way to make sense of this:
"ab" - "cd" // returns NaN
 +  // returns ""  + false // returns "false"
Empty array and Boolean operations returning strings leaves me wondering how strings are implied. In the first case above I'd expect the return of a single array containing values from both arrays, and in the second case an error. Playing with the first case a little further:
[1, 2] + [3, 4] // returns "1,23,4"
Again, I'd prefer an array containing those four numbers rather than the
concatenation. For my wish to be granted I would instead need to do this:
[1, 2].concat([3, 4]) // returns [1, 2, 3, 4]
Yeah, that's weird. I'd like to see the above two examples return what the other returns but as they say: it is what it is. Back to adding an array and a Boolean, again I'd prefer to see an error and not a string containing false. In the same vein if I wanted an array containing a false Boolean I'd think the addition assignment operator (+=) would do the trick:
var array =  array += false array // returns "false"
But it doesn't. Again, another silly string. Oh well, I don't make the rules but here are just a couple more:
typeof NaN // returns "number" NaN === NaN // returns false null < 0 // returns false null == 0 // returns false null <= 0 // returns true
values during development and send front-end errors to your server during production. Who's adding arrays and Booleans, anyway? Maybe use a little Python to clean up your data because as they also say: garbage in, garbage out.
HTML has its weird parts, maybe you didn't know a variable can be defined in HTML with a
tag. There are a number of lesser-known tags like
(check out https://dev.to/christopherkade/unusual-html-tags-worth-knowing-3gbk for more). Adopting their use makes the task of creating search engine indexes (and snippets) easier. You're probably used to wrapping everything in
tags on pages instead of using more descriptive
tags but admittedly, old habits do die hard. CSS has its weird parts too; vertical padding with regard to percentage is relative to the parent element's width, not height. Finally, a little Python:
>>> a = 256 >>> b = 256 >>> a is b True >>> a = 257 >>> b = 257 >>> a is b False >>> a = 257; b = 257 >>> a is b True
I rest my case.
Create your free account to unlock your custom reading experience.