Let's now study the conversion of other
data types to boolean. Such a transformation
is carried out using the function Boolean
.
Here are the values that yield false
when cast to boolean:
console.log(Boolean(0)); // false
console.log(Boolean(-0)); // false
console.log(Boolean(+0)); // false
console.log(Boolean(null)); // false
console.log(Boolean(false)); // false
console.log(Boolean(NaN)); // false
console.log(Boolean(undefined)); // false
console.log(Boolean('')); // false
All other values in JavaScript (other languages don't)
yield true
. Here are the values
that lead to true
, but which may
cause you doubts:
console.log(Boolean(-1)); // true
console.log(Boolean(Infinity)); // true
console.log(Boolean(-Infinity)); // true
The following values are strings because
they are quoted and are also converted to true
:
console.log(Boolean('0')); // true
console.log(Boolean('false')); // true
console.log(Boolean('NaN')); // true
console.log(Boolean('null')); // true
console.log(Boolean('undefined')); // true
Without running the code, determine what will be displayed on the screen:
let test = Boolean(3);
alert(test);
Without running the code, determine what will be displayed on the screen:
let test = Boolean(0);
alert(test);
Without running the code, determine what will be displayed on the screen:
let test = Boolean(-1);
alert(test);
Without running the code, determine what will be displayed on the screen:
let test = Boolean(-0);
alert(test);
Without running the code, determine what will be displayed on the screen:
let test = Boolean(+0);
alert(test);
Without running the code, determine what will be displayed on the screen:
let test = Boolean('abc');
alert(test);
Without running the code, determine what will be displayed on the screen:
let test = Boolean('');
alert(test);
Without running the code, determine what will be displayed on the screen:
let test = Boolean('0');
alert(test);
Without running the code, determine what will be displayed on the screen:
let test = Boolean(true);
alert(test);
Without running the code, determine what will be displayed on the screen:
let test = Boolean(false);
alert(test);
Without running the code, determine what will be displayed on the screen:
let test = Boolean('true');
alert(test);
Without running the code, determine what will be displayed on the screen:
let test = Boolean('false');
alert(test);
Without running the code, determine what will be displayed on the screen:
let test = Boolean(null);
alert(test);
Without running the code, determine what will be displayed on the screen:
let test = Boolean('null');
alert(test);
Without running the code, determine what will be displayed on the screen:
let test = Boolean(undefined);
alert(test);
Without running the code, determine what will be displayed on the screen:
let test = Boolean('undefined');
alert(test);
Without running the code, determine what will be displayed on the screen:
let test = Boolean(NaN);
alert(test);
Without running the code, determine what will be displayed on the screen:
let test = Boolean('NaN');
alert(test);
Without running the code, determine what will be displayed on the screen:
let test = Boolean(3 * 'abc');
alert(test);
Without running the code, determine what will be displayed on the screen:
let test = Boolean(Infinity);
alert(test);
Without running the code, determine what will be displayed on the screen:
let test = Boolean(1 / 0);
alert(test);