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

javascriptBoolean(what to convert);

Example

Let's convert the number 0 to a boolean type:

javascriptBoolean(0);

As a result of executing the code, we will get false:

javascriptfalse

Example

Let's convert the NaN value:

javascriptBoolean(NaN);

Also after executing the code we get false:

javascriptfalse

Example

Now let's convert the number 1:

javascriptBoolean(1);

After executing the code, we get true:

javascripttrue

Example

Now let's convert a negative number:

javascriptBoolean(-1);

As a result of executing the code, we will get true:

javascripttrue

Example

Let's set a parameter to a fractional number:

javascriptBoolean(11.5);

After executing the code, we will also get true:

javascripttrue

Example

Now let's convert an empty string:

javascriptBoolean('');

The code execution result:

javascriptfalse

Example

If we enter a filled string in the parameter:

javascriptBoolean('str');

Then after conversion we get true:

javascripttrue

Example

Let's convert the boolean true value:

javascriptBoolean(true);

The code execution result:

javascripttrue

Example

Now let's convert the boolean false value:

javascriptBoolean(false);

As a result of executing the code, false is returned to us:

javascriptfalse

Example

Let's specify an empty object in the function parameter:

javascriptBoolean({});

After executing the code, the Boolean function will convert the object to the true value:

javascripttrue

Example

Now we specify an empty array in the parameter:

javascriptBoolean([]);

After executing the code, we will also get the true value:

javascripttrue

Example

Let's convert an empty function:

javascriptBoolean(function(){});

As a result of code execution, we get true:

javascripttrue

Example

Now let's convert the null value:

javascriptBoolean(null);

As a result of the code execution, we get false as well as when converting the number 0:

javascript            false

See also

  • the Number function
    that converts to a number
  • the String function
    that converts to a string