Boolean function
The Boolean function converts
a passed value to a boolean. The
result of the function execution
will be either
true,
or false.
In this case, only the following are
converted to false: 0,
an empty string,
null,
undefined,
NaN.
All other values (numbers, strings,
objects, arrays, functions) are
converted to true.
Syntax
Boolean(what to convert);
Example
Let's convert the number
0 to a boolean type:
Boolean(0);
As a result of executing the code,
we will get false:
false
Example
Let's convert the NaN value:
Boolean(NaN);
Also after executing the code
we get false:
false
Example
Now let's convert the
number 1:
Boolean(1);
After executing the code,
we get true:
true
Example
Now let's convert a negative number:
Boolean(-1);
As a result of executing the
code, we will get true:
true
Example
Let's set a parameter to a fractional number:
Boolean(11.5);
After executing the code, we
will also get true:
true
Example
Now let's convert an empty string:
Boolean('');
The code execution result:
false
Example
If we enter a filled string in the parameter:
Boolean('str');
Then after conversion we get
true:
true
Example
Let's convert the boolean
true value:
Boolean(true);
The code execution result:
true
Example
Now let's convert the boolean
false value:
Boolean(false);
As a result of executing the code,
false is returned to us:
false
Example
Let's specify an empty object in the function parameter:
Boolean({});
After executing the code, the
Boolean function will
convert the object to the
true value:
true
Example
Now we specify an empty array in the parameter:
Boolean([]);
After executing the code,
we will also get the
true value:
true
Example
Let's convert an empty function:
Boolean(function(){});
As a result of code execution,
we get true:
true
Example
Now let's convert the
null value:
Boolean(null);
As a result of the code execution,
we get false as well as when
converting the number 0:
false