I\'ve a function read()
that takes as a boolean paramater. If false
is passed in - read(false)
- it shouldn\'t run a block of code. It wo
It's because the concept automatic conversion in Javascript, the undefined
value convert to false
. So three lines are similar to ensure the variable first_time_here
is false
, not undefined
.
if first_time_here
is undefined
:
first_time_here = undedined !== false -> first_time_here = false != false
-> first_time_here = false;
And:
first_time_here = undedined || false -> first_time_here = false || false
-> first_time_here = false;
If you're expecting falsey values, use typeof
:
var x = typeof x !== 'undefined' ? x : false;
Otherwise, if you do this:
var x = x || true;
And pass in false
, the value of x
will be true
.