3 ms·
"This sort of thing happens in type conversion languages. You can either use === to stop conversion or you can understand how conversion works." Even JavaScrip
by Nemcue 11y ago
"This sort of thing happens in type conversion languages. You can either use === to stop conversion or you can understand how conversion works."
Even JavaScript isn't insane enough to somehow coerce a string to 0.
- adamzochowski 11y agoYes, JavaScript will convert strings into numbers console.log(5*"12"); 60 console.log(5*"0x0C"); 60
- sukilot 11y ago12 is not 0.
- Navarr 11y agoYes and no. JavaScript gives you the good ol' "NaN" which is a number (despite not being a number). PHP doesn't have that concept in it.
- DonHopkins 11y agogNaN's Not a Number
- grkvlt 11y agoSort of. 'NaN' is one of the IEEE 754 floating point constants, along with 'Inf' for infinity. They are numeric types, in that they can be returned via operations on numbers, such as dividing zero by zero or adding '-Inf' to 'Inf'. See https://en.wikipedia.org/wiki/NaN https://en.wikipedia.org/wiki/NaN I always understood that the 'isNaN()' function was required to check if a numeric variable is equal to 'NaN' directly, since normal equality cannot be used as there are multiple valid bitwise representations of 'NaN' in the standard - it is a float with an exponent of all ones and a non-zero fraction. However, 'isNaN()' now seems to have been co-opted into being used to check if a string is not a number, i.e. does not represent a numeric value, and in fact I believe this is now the documented description of the function in ECMAScript?
- fidz 11y agoActually, sometimes type conversion make some code become a little bit handy. We use Java at the backend and of course Javascript for frontend. When serializing, in Java we should String dataRaw = "42"; int objectId = Integer.parseInt(dataRaw); Meanwhile, in JS, it is fairly simple: dataRaw = "42"; var objectid = +dataRaw;