0

For context you can find the full class definition @ https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch6.md

I am trying to understand this particular expression found under "Simpler Design"

LoginController.prototype.validateEntry = function(user,pw) { user = user || this.getUser(); //this is the statement I can't follow pw = pw || this.getPassword();

If user = undefined or user = "", then user = user //false. For any string values user = user //true. But in these cases user = user; evaluates to the same value as simply user;

1) is there any added functionality by writing user = user; instead of just user;?

2) why is the JavaScript engine able to evaluate a LHS assignment as a boolean value?

Phil Dwan
  • 71
  • 1
  • 7
  • You have your operator precedence wrong. It makes much more sense when you put the operations in the right order. – Quentin Nov 29 '17 at 22:19

1 Answers1

0

In javascript, when you do || it's not necessarily returning a boolean, it will return the first non-falsy value.

It's basically saying set user to either the current value of user, or, if that is null, false, or undefined, set it to whatever this.getUser() returns.

dave
  • 50,635
  • 4
  • 62
  • 77