Return Boolean from Function in JavaScript

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.

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:

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 or No
  • True or False
  • 0 or 1

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.

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, and NaN, other numbers are true.
  • Symbols turn into true.
  • All objects become 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)

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 the if statement with the === operator.
  • Depending on the if condition, return true or false.
    Strict equality operator === returns either true or false depending on the value of two operands.

We can also compare without using the if-else block, giving the same result as the above example.

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.

We can also write it using the arrow function as follows:

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.

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.

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:

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.

We can see that the output is false when we compare two objects. Now, let’s see how the === operator will behave.

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

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *