js
- null and undefied (PJ3.4.3)
you should never explicitly set the value of a variable to undefined, but the same does not hold true for null. Any time an object is expected but is not available, null should be used in its place.
Another thing, Number(null) returns 0 while Number(undefied) returns NaN.
- use "or" operator ("||") to set a default value, use "and" operator ("&&") to check before using. e.g.:
var middle = stooge["middle-name"] || "(none)";
flight.equipement && flight.equipement.model;
- isNaN
isNaN("true") //true, Boolean to number
isNaN("10") //true, string "10" to 10
Although typically not done, isNaN() can be applied to objects. In that case, the
object’s valueOf() method is fi rst called to determine if the returned value can
be converted into a number. If not, the toString() method is called and its
returned value is tested as well. This is the general way that built-in functions
and operators work in ECMAScript
- Number() vs parseInt()
Number(null) //0
parseInt(null) //NaN
parseInt("070")
how strange, PJ says:
In ECMAScript 3 JavaScript engines, the value “070” is treated as an octal literal and becomes the decimal value 56. In ECMAScript 5 JavaScript engines, the ability to parse octal values has been removed from parseInt() , so the leading zero is considered invalid and the value is treated the same as “0”, resulting in the decimal value 0. This is true even when running ECMAScript 5 in non-strict mode.
but I tried on my chrome and and firefox, parseInt("070") just returns 70 instead of 56. So I'd better always specify the base...