I encountered this piece of code:
var value = this.newTodo && this.newTodo.trim();
In this example: http://codepen.io/anon/pen/rONEMQ?editors=101
I don't understand what the AND (&&) operator is trying to accomplish.
In my understanding the && Operator is to asign multiple properties to a value, but this doesn't seem to be the case here.
I my code example, I don't see any difference, for when I use this:
var value = this.newTodo && this.newTodo.trim();
Instead of this:
var value = this.newTodo;
Or just this:
var value = this.newTodo.trim();
Can anyone explain, what the initial line stated above and in the example does with the &&?
var value = this.newTodo && this.newTodo.trim();
if this.newTodo is not one of "falsey" values, i.e. ""
, 0
, false
, undefined
, null
, []
(I'm sure I'm forgetting some), then value = this.newTodo.trim()
, otherwise value = this.newTodo
(which could be ""
, 0
, false
, undefined
, null
, []
etc)
I my code example, I don't see any difference, for when I use this:
var value = this.newTodo && this.newTodo.trim();
Instead of this:
var value = this.newTodo;
if this.newTodo is "Hello World"
... you're right, there is no difference
if this.newTodo id " Hello World "
... value will be "Hello World"
... i.e. trimmed of leading and trailing spaces