arrays - JavaScript null and plus (+) operatior -
i trying understand core of javascript. know doesnt have implementation value. if dont want answer, leave it. however, appreciate if understand following type coercion while applying addition(+).
1. null + null // 0 2. null + undefined; // nan 3. null + nan; // nan 4. 1 + null; //1 5. true + null; //1 6. true + [null]; //"true" i know null empty or missing object. appreciate, if can explain steps in type coercion or unary(+) operation here. reading question.
this covered in 11.6.1 addition operator ( + ) - feel free read , follow rules.
the first 5 cases can explained looking @ tonumber:
value tonumber(value) --------- --------------- null 0 undefined nan nan nan 1 1 true 1 and 0 + 0 == 0 (and 1 + 0 == 1), while x + nan or nan + x evaluates nan. since every value above primitive, toprimitive(x) evaluates x (where x not string) , "string concatenation clause" not invoked.
the final case different in results toprimitive (which ends calling array.prototype.tostring) on array results in string value. ends applying tostring, not tonumber, , follows such:
true + [null] => true + "" // after toprimitive([null]) => "" => "true" + "" // after tostring(true) => "true" => "true" // via string concatenation
Comments
Post a Comment