javascript - why expression with 'undefined' does not return boolean -


this question has answer here:

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); }; 

here’s it’s specified.

the production logicalandexpression : logicalandexpression && bitwiseorexpression evaluated follows:

  1. let lref result of evaluating logicalandexpression.
  2. let lval getvalue(lref).
  3. if toboolean(lval) false, return lval.
  4. let rref result of evaluating bitwiseorexpression.
  5. 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

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -