First off, the facts:

if (booleanValue)

Will satisfy the if statement for any truthy value of booleanValue including true, any non-zero number, any non-empty string value, any object or array reference, etc...

On the other hand:

if (booleanValue === true)

This will only satisfy the if condition if booleanValue is exactly equal to true. No other truthy value will satisfy it.

On the other hand if you do this:

if (someVar == true)

Then, what Javascript will do is type coerce true to match the type of someVar and then compare the two variables. There are lots of situations where this is likely not what one would intend. Because of this, in most cases you want to avoid == because there's a fairly long set of rules on how Javascript will type coerce two things to be the same type and unless you understand all those rules and can anticipate everything that the JS interpreter might do when given two different types (which most JS developers cannot), you probably want to avoid == entirely.

As an example of how confusing it can be:

var x;

x = 0;
console.log(x == true);   // false, as expected
console.log(x == false);  // true as expected

x = 1;
console.log(x == true);   // true, as expected
console.log(x == false);  // false as expected

x = 2;
console.log(x == true);   // false, ??
console.log(x == false);  // false 

For the value 2, you would think that 2 is a truthy value so it would compare favorably to true, but that isn't how the type coercion works. It is converting the right hand value to match the type of the left hand value so its converting true to the number 1 so it's comparing 2 == 1 which is certainly not what you likely intended.

So, buyer beware. It's likely best to avoid == in nearly all cases unless you explicitly know the types you will be comparing and know how all the possible types coercion algorithms work.


So, it really depends upon the expected values for booleanValue and how you want the code to work. If you know in advance that it's only ever going to have a true or false value, then comparing it explicitly with

if (booleanValue === true)

is just extra code and unnecessary and

if (booleanValue)

is more compact and arguably cleaner/better.

If, on the other hand, you don't know what booleanValue might be and you want to test if it is truly set to true with no other automatic type conversions allowed, then

if (booleanValue === true)

is not only a good idea, but required.


For example, if you look at the implementation of .on() in jQuery, it has an optional return value. If the callback returns false, then jQuery will automatically stop propagation of the event. In this specific case, since jQuery wants to ONLY stop propagation if false was returned, they check the return value explicity for === false because they don't want undefined or 0 or "" or anything else that will automatically type-convert to false to also satisfy the comparison.

For example, here's the jQuery event handling callback code:

ret = ( specialHandle || handleObj.handler ).apply( matched.elem, args );

if ( ret !== undefined ) {
     event.result = ret;
     if ( ret === false ) {
         event.preventDefault();
         event.stopPropagation();
     }
 }

You can see that jQuery is explicitly looking for ret === false.

But, there are also many other places in the jQuery code where a simpler check is appropriate given the desire of the code. For example:

// The DOM ready check for Internet Explorer
function doScrollCheck() {
    if ( jQuery.isReady ) {
        return;
    }
    ...
Answer from jfriend00 on Stack Overflow
Top answer
1 of 14
305

First off, the facts:

if (booleanValue)

Will satisfy the if statement for any truthy value of booleanValue including true, any non-zero number, any non-empty string value, any object or array reference, etc...

On the other hand:

if (booleanValue === true)

This will only satisfy the if condition if booleanValue is exactly equal to true. No other truthy value will satisfy it.

On the other hand if you do this:

if (someVar == true)

Then, what Javascript will do is type coerce true to match the type of someVar and then compare the two variables. There are lots of situations where this is likely not what one would intend. Because of this, in most cases you want to avoid == because there's a fairly long set of rules on how Javascript will type coerce two things to be the same type and unless you understand all those rules and can anticipate everything that the JS interpreter might do when given two different types (which most JS developers cannot), you probably want to avoid == entirely.

As an example of how confusing it can be:

var x;

x = 0;
console.log(x == true);   // false, as expected
console.log(x == false);  // true as expected

x = 1;
console.log(x == true);   // true, as expected
console.log(x == false);  // false as expected

x = 2;
console.log(x == true);   // false, ??
console.log(x == false);  // false 

For the value 2, you would think that 2 is a truthy value so it would compare favorably to true, but that isn't how the type coercion works. It is converting the right hand value to match the type of the left hand value so its converting true to the number 1 so it's comparing 2 == 1 which is certainly not what you likely intended.

So, buyer beware. It's likely best to avoid == in nearly all cases unless you explicitly know the types you will be comparing and know how all the possible types coercion algorithms work.


So, it really depends upon the expected values for booleanValue and how you want the code to work. If you know in advance that it's only ever going to have a true or false value, then comparing it explicitly with

if (booleanValue === true)

is just extra code and unnecessary and

if (booleanValue)

is more compact and arguably cleaner/better.

If, on the other hand, you don't know what booleanValue might be and you want to test if it is truly set to true with no other automatic type conversions allowed, then

if (booleanValue === true)

is not only a good idea, but required.


For example, if you look at the implementation of .on() in jQuery, it has an optional return value. If the callback returns false, then jQuery will automatically stop propagation of the event. In this specific case, since jQuery wants to ONLY stop propagation if false was returned, they check the return value explicity for === false because they don't want undefined or 0 or "" or anything else that will automatically type-convert to false to also satisfy the comparison.

For example, here's the jQuery event handling callback code:

ret = ( specialHandle || handleObj.handler ).apply( matched.elem, args );

if ( ret !== undefined ) {
     event.result = ret;
     if ( ret === false ) {
         event.preventDefault();
         event.stopPropagation();
     }
 }

You can see that jQuery is explicitly looking for ret === false.

But, there are also many other places in the jQuery code where a simpler check is appropriate given the desire of the code. For example:

// The DOM ready check for Internet Explorer
function doScrollCheck() {
    if ( jQuery.isReady ) {
        return;
    }
    ...
2 of 14
53

If you write: if(x === true) , It will be true for only x = true

If you write: if(x) , it will be true for any x that is not: '' (empty string), false, null, undefined, 0, NaN.

🌐
W3Schools
w3schools.com › js › js_booleans.asp
JavaScript Booleans
if (hour < 18) { greeting = "Good day"; } else { greeting = "Good evening"; } Try it Yourself » ... Booleans are extensively used in loops to determine conditions for looping. while (i < 10) { text += i; i++; } Try it Yourself » ... 100 is true 3.14 is true -15 is true true is true "Hello" is true "false" is true (7 + 1 + 3.14) is true Try it Yourself »
Discussions

How do boolean values work with conditional statements?
Hey, just want to double check my foundational knowledge with javascript, especially in regards to boolean values and conditional statements · if a boolean value is initially set as "false", then later down the line, changes to "true" - if the boolean value is used in an "if" statement, and ... More on teamtreehouse.com
🌐 teamtreehouse.com
2
September 14, 2016
Should I say boolean === true/false in an if statement?

Depends on the context.

Generally, using truthiness (like in the second block) is idiomatic. You don't need to be defensive about undefined variables if you never expect to work with them.

But, if the null/undefined/etc. case is expected and should be handled differently, then the first block is the idiomatic way of checking for exactly false. These cases are rare, however, and a code block where a certain variable might or might not be defined smells bad.

If you're worried about a case where undefined might arise accidentally (e.g., spelling a variable name incorrectly), consider using a tool like JSHint, which will reliably catch such cases without requiring you to change your style.

More on reddit.com
🌐 r/javascript
60
50
August 23, 2014
javascript - How to check if type is Boolean - Stack Overflow
The most readable: val === false || val === true. Also readable: typeof variable == typeof true. The shortest, but not readable at all: !!val === val. ... [===] The triple equals test for strict equality: both the type (Boolean) and the value have to be the same. If the original value is not ... More on stackoverflow.com
🌐 stackoverflow.com
Javascript check if string if true or false and convert to Boolean - Stack Overflow
Is there a better way to improve the below statement to check if the val() is 'true' or 'false' and if it is then it will change it to a Boolean. The reason being, some of the values may be a word.... More on stackoverflow.com
🌐 stackoverflow.com
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Boolean
Boolean - JavaScript - MDN Web Docs
There are two ways to achieve the same effect in JavaScript. Double NOT: !!x negates x twice, which converts x to a boolean using the same algorithm as above. The Boolean() function: Boolean(x) uses the same algorithm as above to convert x. Note that truthiness is not the same as being loosely equal to true or false. ... if ([]) { console.log("[] is truthy"); } if ([] == false) { console.log("[] == false"); } // [] is truthy // [] == false
🌐
Exploring JS
exploringjs.com › js › book › ch_booleans.html
Booleans • Exploring JavaScript (ES2025 Edition)
If the first operand is truthy, console.log() is executed: ... Evaluate a. Is the result falsy? Return it. Otherwise, evaluate b and return the result. In other words, the following two expressions are roughly equivalent: ... > false && true false > false && 'abc' false > true && false false > true && 'abc' 'abc' > '' && 'abc' ''
🌐
Reddit
reddit.com › r/javascript › should i say boolean === true/false in an if statement?
r/javascript on Reddit: Should I say boolean === true/false in an if statement?
August 23, 2014 -

I've started doing this for boolean variables:

if (booleanVariable === false) { //intent: if boolean variable exists and is false

}

(I think) This proves to me that booleanVariable is defined and is false. But coming from Java, it's a little weird to write code this way. Normally you'd say

if (!booleanVariable)

What's the idiomatic way to write the first block?

Top answer
1 of 16
843

That's what typeof is there for. The parentheses are optional since it is an operator.

if (typeof variable == "boolean") {
    // variable is a boolean
}
2 of 16
86

With pure JavaScript, you can simply use typeof and do something like typeof false or typeof true and it will return "boolean"...

But wait, that's not the only way to do that, I'm creating functions below to show different ways you can check for Boolean in JavaScript, also different ways you can do it in some new frameworks, let's start with this one:

function isBoolean(val) {
   return val === false || val === true;
}

Or one-line ES6 way ...

const isBoolean = val => 'boolean' === typeof val;

and call it like!

isBoolean(false); //return true

Also in Underscore source code they check it like this(with the _. at the start of the function name):

isBoolean = function(obj) {
   return obj === true || obj === false || toString.call(obj) === '[object Boolean]';
};

Also in jQuery you can check it like this:

jQuery.type(true); //return "boolean"

In React, if using propTypes, you can check a value to be boolean like this:

MyComponent.propTypes = {
  children: PropTypes.bool.isRequired
};

If using TypeScript, you can use type boolean also:

let isDone: boolean = false;

Also another way to do it, is like converting the value to boolean and see if it's exactly the same still, something like:

const isBoolean = val => !!val === val;

or like:

const isBoolean = val => Boolean(val) === val;

and call it!

isBoolean(false); //return true

It's not recommended using any framework for this as it's really a simple check in JavaScript.

🌐
SheCodes
shecodes.io › athena › 2396-checking-boolean-statements-in-javascript
[JavaScript] - Checking Boolean Statements in Javascript - | SheCodes
Learn how to check boolean values in JavaScript using the console.log command and discover more useful JavaScript rituals from MDN web docs.
Find elsewhere
🌐
JavaScript in Plain English
javascript.plainenglish.io › how-to-check-for-a-boolean-in-javascript-98fdc8aec2a7
How to Check for a Boolean in JavaScript | by Dr. Derek Austin 🥳 | JavaScript in Plain English
January 4, 2023 - How to Check for a Boolean in JavaScript Boolean values, true or false, are one of the easiest primitives to check for in JavaScript — just use the typeof operator. Booleans are true or …
🌐
Codidact
software.codidact.com › posts › 291489 › 291490
In javascript is there really a good reason to never check for boolean true || false such as if(var){}else{}? - Software Development
In JavaScript, when you have if (someVar), the variable someVar can be basically "anything" - even a boolean value! If someVar is not a boolean, it'll be converted according to some rules: Booleans are returned as-is.
🌐
TutorialsPoint
tutorialspoint.com › How-to-check-if-a-variable-is-boolean-in-JavaScript
How to check if a variable is boolean in JavaScript?
August 8, 2022 - In the below example, we have compared the bool name variable with the true and false Boolean values. Users can see the result in the output. <html> <head> <title>Check if variable is of boolean type</title> </head> <body> <h2>Check if variable is of boolean type in JavaScript using <i> strict equality </i> operator.</h2> <div id = "result1"></div> <script> let result1 = document.getElementById("result1"); let bool = true; if (bool === true || bool === false) { result1.innerHTML = "variable bool is type of boolean."; } else { result1.innerHTML = "variable bool is not type of boolean."; } </script> </body> </html>
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-check-if-type-boolean
How to check if type is Boolean using JavaScript | bobbyhadz
March 3, 2024 - The isBoolean function takes a value as a parameter and returns true if the value is of type boolean and false otherwise. An alternative approach is to use the logical OR (||) operator.
🌐
Stack Abuse
stackabuse.com › bytes › javascript-check-if-all-values-in-array-are-true-or-false
JavaScript: Check if all Values in Array are True or False
August 16, 2023 - If all elements are truthy, it returns true; otherwise, it returns false. On the other hand, a "falsy" value is a value that is considered false when encountered in a Boolean context. JavaScript recognizes false, 0, -0, 0n, "", null, undefined, and NaN as falsy.
🌐
W3Resource
w3resource.com › javascript-exercises › validation › javascript-validation-exercise-1.php
JavaScript validation: Validate whether a given value type is Boolean or not - w3resource
<!DOCTYPE html> <html> <head> <meta ... a JavaScript function that checks if a given input is strictly of type boolean using the typeof operator and returns true or false....
🌐
Linux Hint
linuxhint.com › check-if-type-is-boolean-using-javascript
How to Check if the Type is Boolean Using JavaScript
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
Team Treehouse
teamtreehouse.com › library › javascript-basics › boolean-values
Boolean Values (How To) | JavaScript Basics | Treehouse
First, the if statement will check if the value of correctGuess is true. 4:53 · If it's true, then log the message, You guessed the number. 5:00 ... Before running it, let's step through this code line by line. 5:34 · First, we declare a variable with the Boolean value false.
Published   February 18, 2020
🌐
Code The Web
codetheweb.blog › if-statements-boolean-operators
All about IF statements and booleans in JavaScript! • Code The Web
February 28, 2018 - Here, we’re telling the computer to return true if 1 + 3 is not equal to 4. 1 + 3 is obviously equal to 4, so it returns false. There are a few other comparisons that we can use - here is a list of the basic comparison signs: Let’s try a few of them out… Challenge Time! Question 1: Alert “Not fake news!” if 4 is less than 410. ... Question 2: Alert “5 is the greatest!” if 5 is greater 6. ... Question 3: Alert “JavaScript is awesome!” if 3 is less than or equal to 3.