Re:ES6 in Depth – Symbols
by IshKebab on Friday June 12, 2015 @08:17AM
(#9705375)
I wish javascript had some kind of flag so you could get it to act sanely. All the comparison operators would work like they do in sane languages; type conversion wouldn't be quite so automatic and insane, etc.
[ Parent
| on HN ]
Re:ES6 in Depth – Symbols
by jordanwallwork on Friday June 12, 2015 @08:25AM
(#9705410)
If you just make sure to always use === then this is exactly what happens. You can use tools like jslint to warn you if you accidentally use ==
Not quite though. I had a bug with Javascript where I was reading in a number and forgot to parse it to a float. I ended up doing an addition with that value, which later got used as a float again. So I had 1 + "10" turn to 110 when I tried to use it. No == or === anywhere.
Re:ES6 in Depth – Symbols
by jordanwallwork on Friday June 12, 2015 @11:34AM
(#9706503)
I was only really referring to automatic type conversion for comparisons. That said, the example you gave sounds like the opposite, where you wanted it to do some auto-conversion and you're disappointed that it didn't? If I did 1 + "10" I don't think I'd want it to return 11!
I'd want it to throw some kind of cast exception, personally, not try and do what it thinks I want. (I'm not the GP)
It did convert the value. It converted the 1 to a string and added "10" to the end of it, resulting in "110" which then was converted to a float and used.
The error in this case would be forgetting Javascript's rules of implicit conversion.
Adding a float to a string results in a string.