Table of Contents
Using the Boolean() Function
To get a Boolean from a function in JavaScript:
- Create a function which returns a Boolean value.
- Use the
Boolean()
function to get a Boolean value from the function created in the previous step.
1 2 3 4 5 6 |
function func(){ return Boolean(10>5); } console.log(func()); |
1 2 3 |
true |
We used the Boolean()
, a built-in function in JavaScript, to check if a variable or an expression is true
or false
and returned its value using the return
statement. Also, we can write the above example without Boolean()
like:
1 2 3 4 5 6 |
function func(){ return (10>5); } console.log(func()); |
1 2 3 |
true |
If you are not aware of Boolean values and want to know how we can declare them, then we have you covered. A Boolean value can be one of these in programming languages:
Yes
orNo
True
orFalse
0
or1
However, there is only one data type for Boolean values in JavaScript: true
or false
. In JavaScript, we can declare a boolean variable with and without using let/var
. Have a look at the example below to see JavaScript compiler behaviour.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function boolfunc(){ return true; } isBoolean1 = boolfunc(); console.log(typeof(isBoolean1)); let isBoolean2 = boolfunc(); console.log(typeof(isBoolean2)); var isBoolean3 = boolfunc(); console.log(typeof(isBoolean3)); |
1 2 3 4 5 |
boolean boolean boolean |
We can observe that the variable’s data type declared with/without let/var
is a boolean
.
Use the Boolean()
Function with Truthy/Falsy Values
Some values are Truthy Values
in JavaScript, which are always true
; we can find these values below:
- Except
0
,-0
, andNaN
, other numbers aretrue
. Symbols
turn intotrue
.- All
objects
becometrue
.
1 2 3 4 5 6 7 8 9 10 |
console.log("10 is " + Boolean(100)); console.log("1.6 is " + Boolean(3.14)); console.log("-15 is " + Boolean(-15)); console.log(Boolean("Hello")); console.log(Boolean('false')); console.log(Boolean(1 + 7 + 3.14)); console.log(Boolean([])); console.log(Boolean({})); |
1 2 3 4 5 6 7 8 9 10 |
10 is true 1.6 is true -15 is true true true true true true |
We can observe above that anything with some value, including expressions except 0
, was true
. Note that even the string false
was also true
. In JavaScript, the falsy values
are those that always return false
; these values are listed below:
false
null
undefined
0
""
(empty string)
NaN (not a number)
1 2 3 4 5 6 7 |
console.log(Boolean('')); console.log(Boolean(0)); console.log(Boolean(-0)); console.log(Boolean(undefined)); console.log(Boolean(null)); |
1 2 3 4 5 6 7 |
false false false false false |
So now, we can define a function based on the type of argument it is getting and wrap our return value in the Boolean()
function to return a Boolean value from a function.
Using Comparison Operator
To get a Boolean value from a function:
- Create a function
isEqual()
and evaluate its value using theif
statement with the===
operator. - Depending on the
if
condition, returntrue
orfalse
.
Strict equality operator===
returns eithertrue
orfalse
depending on the value of two operands.
1 2 3 4 5 6 7 8 9 10 11 12 |
function isEqual(x,y) { if (x === y) { return true; } else { return false; } } console.log(isEqual(12,6)) console.log(isEqual(5,5)) |
1 2 3 4 |
false true |
We can also compare without using the if-else
block, giving the same result as the above example.
1 2 3 4 5 6 7 8 |
function isEqual(x,y) { return x === y; } console.log(isEqual(12,6)); console.log(isEqual(5,5)); |
1 2 3 4 |
false true |
Now, let’s create another function related to a real-life example that determines whether someone is eligible for a job and see how to return a Boolean value from a function there.
The isEligibleForJob()
function takes an age
argument and returns whether the person is old enough to apply for a job. First, a check is made to see if the age
is equal to or greater than 25. Based on that comparison, it will return true
or false
.
1 2 3 4 5 6 7 8 |
function isEligibleForJob(age) { return age >= 25; } console.log(isEligibleForJob(18)); console.log(isEligibleForJob(25)); |
1 2 3 4 |
false true |
We can also write it using the arrow function
as follows:
1 2 3 4 5 |
const isEligibleForJob = (age) => age >= 25; console.log(isEligibleForJob(18)); // false console.log(isEligibleForJob(25)); // true |
In the above code, =>
represented the Fat Arrow
of an arrow function while >=
is an operator which means greater-than and equal-to. Alternatively, we can also use the Ternary operator ?:
and an Arrow Function
to return a Boolean value from a function.
1 2 3 4 5 |
const isEligibleForJob = (age) => age < 25 ? false: true; console.log(isEligibleForJob(18)); //false console.log(isEligibleForJob(25)); //true |
We noticed that the above code returned two Boolean values, false
and true
. Now, think of a scenario where we have to work with Booleans as objects and return a Boolean value after making a comparison with those objects in a function. Let’s learn how we can do it.
Use ==
/===
Operator to Get Boolean Using Booleans as Objects
To get a Boolean using Booleans as objects:
- Write a function which takes a Boolean type value as an argument.
- Use the
new
keyword to create a Boolean object. - Use
==
operator to do comparison and return the resulted value.
1 2 3 4 5 6 7 8 |
function comparison(x) { let y = new Boolean(false); return x==y; } console.log(comparison(false)); |
1 2 3 |
true |
In JavaScript, we can define an object using a new
keyword. In the above example, we wrote a function comparison()
, which took a variable x
as an argument and created an object y
.
After creating an object, it compared x
(Boolean value) and y
(Boolean object) to see what Boolean value it would return. After comparison, we received true
as an output.
Similarly, we can use ===
operator as follows:
1 2 3 4 5 6 7 8 |
function comparison(x) { let y = new Boolean(false); return x===y; } console.log(comparison(false)); |
1 2 3 |
false |
Now observe the result using the ===
operator; x
(a Boolean value) and y
(a Boolean object) are not equal. It is because the ===
operator not only compared the values but their data type as well.
There can be a situation where we want to get a Boolean value from a function after comparing two Boolean objects. Let’s see how we can do it using comparison operators.
Use ==
/===
Operator to Compare Two Boolean Objects
To get a Boolean value from a function after comparing two Boolean objects:
- Write a function which creates two Boolean-type objects using the
new
keyword. - Use
==
operator to compare both boolean objects and return resulted value. - Now, call the function (created in the first step) to see what it returns after comparing two Boolean objects.
1 2 3 4 5 6 7 8 9 |
function compareObjects() { let y = new Boolean(true); //y is boolean object let x = new Boolean(false) ; // x is a boolean object return x==y; } console.log(compareObjects()); //false |
We can see that the output is false
when we compare two objects. Now, let’s see how the ===
operator will behave.
1 2 3 4 5 6 7 8 9 |
function compareObjects() { let y = new Boolean(true); //y is boolean object let x = new Boolean(false); // x is a boolean object return x===y; } console.log(compareObjects());//false |
It also returned false
. We can observe comparing two JavaScript objects that always return a false
value. That is the reason avoiding Boolean objects is good because it gives unexpected results. Making a comparison of Boolean variables and Boolean objects is not safe to do.
That’s all about how to return boolean from function in JavaScript