Check If A Function Returns True In JavaScript

Check if a Function returns True in JavaScript

When working with JavaScript functions, we often want to Check if a Function returns True in JavaScript. This tutorial will help you with two methods: use the if statement and use the ternary operator.

How to check if a function returns True in JavaScript?

There is only one way to check if a function returns true: calling the function and seeing if its value returned is equal to the boolean true or not.

Using if statement

Any programmer starting with a programming language has gotten used to the if statement as it is one of the most basic knowledge in JavaScript. If you want to check if the function returns the true boolean value (same type and same value) precisely, you should use the strict equal operator (===) to check it, for example:

// Declare the function to test
function learnshareit() {
     return "LearnShareIT"
}

// Check if a Function returns True
if (learnshareit()===true)
    console.log(true)
else console.log(false)

Output:

false

Another example:

// Declare the function to test
function learnshareit() {
    return true
}

// Check if a Function returns True
if (learnshareit()===true)
    console.log(true)
else console.log(false)

Output:

true

As you can see, the function returns a string instead of true in the first example, so the result is false. However, the second example has a function that returns true whenever called; hence, the output is true. However, the strict equal checks the type of the return value also. Therefore if the function does not return a boolean but a truthy or false value, it will be marked as incorrect. If you replace the strict equal with the equals to (==) operator, it will convert the result of the function into truthy or falsy values before checking, for example:

// Declare the function to test
function learnshareit() {
    return 1
}

// Check if a Function returns truthy value
if (learnshareit()==true)
    console.log(true)
else console.log(false)

// Check if a Function returns exact boolean true
if (learnshareit()===true)
    console.log(true)
else console.log(false)

Output:

true
false

The example above indicates that the function returns 1 instead of true. However, the first if statement checks and prints out true because 1 has been converted to a truthy value, and so it is correct. The second if statement also uses the === operator to check it type, and because 1 is of a numeric type, not boolean like true, the output is false.

Using ternary operator

The ternary operator works precisely like the if statement but is more straightforward than it:

// Declare the function to test
function learnshareit() {
    return 1
}

// Check if a Function returns truthy value
learnshareit()==true?console.log(true):console.log(false)

// Check if a Function returns exact boolean true
learnshareit()===true?console.log(true):console.log(false)

Output:

true
false

Another example:

// Declare the function to test
function learnshareit() {
    return true
}

// Check if a Function returns truthy value
(learnshareit()==true)?console.log(true):console.log(false)

// Check if a Function returns exact boolean true
learnshareit()===true?console.log(true):console.log(false)

Output:

true
true

The logic behind this method is easy to understand. The ternary operator works like the if statement. First you put the condition before the ‘?’ operator, and then after the question mark, you declare the function you want to execute when the condition is correct. After that, you type a colon (:) to declare the second option. This statement will be executed whenever the condition (before ?) is not matched.

Summary

In this tutorial, we have introduced some ways to check if a function returns true in JavaScript. We recommend you use the second approach because it is faster and more straightforward than the first one. If you have a better solution, you should share it with us in reply. Have a good day!

Maybe you are interested:

Leave a Reply

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