javascript - why expression with 'undefined' does not return boolean -
this question has answer here:
- javascript , operator within assignment 6 answers
my questions might silly, why in expression
var query; var n = query && query.length > 0;
n 'undefined' , not false? expected expression evaluates boolean. made me curious.
&&
evaluates left operand. if left operand falsy, evaluates left operand. otherwise, evaluates right operand.
since javascript doesn’t have strong typing, works in same way boolean result part, , allows convenient tricks, one, ||
:
var requestanimationframe = requestanimationframe || window.webkitrequestanimationframe || window.mozrequestanimationframe || window.msrequestanimationframe || window.orequestanimationframe || function (callback) { settimeout(callback, 16); };
the production logicalandexpression : logicalandexpression && bitwiseorexpression evaluated follows:
- let lref result of evaluating logicalandexpression.
- let lval getvalue(lref).
- if toboolean(lval) false, return lval.
- let rref result of evaluating bitwiseorexpression.
- return getvalue(rref).
see note @ bottom of section, directly addresses question!
note the value produced
&&
or||
operator not of type boolean. value produced value of 1 of 2 operand expressions.
Comments
Post a Comment