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
All JavaScript comparison operators (like ==, !=, <, >) return true or false from the comparison. Given that x = 5, the table below explains comparison: let x = 5; (x == 8); // equals false (x != 8); // equals true Try it Yourself » ... Booleans are extensively used in if statements to determine the code blocks to execute based on the logic.
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
Why do we write `if(sale)` instead of `if(sale===true)`?
Not quite understanding why wouldn’t write If (sale === true) instead of if (sale) at the beginning. It doesn’t seem to make sense. Can anyone explain the logic there? I let sale = true; sale = false; if(sale) { console.log(‘Time to buy!’); } else { console.log (‘Time to wait for ... More on discuss.codecademy.com
🌐 discuss.codecademy.com
0
1
April 8, 2019
How do I use booleans with conditional statements in javascript?
Dee Pei is having issues with: I didn't see this addressed anywhere in the video or online. More on teamtreehouse.com
🌐 teamtreehouse.com
3
August 18, 2015
javascript - In JS if (condition) means == true or === true - Stack Overflow
I know the difference between == and === however I always believed that if (condition) condition was supposed to be evaluated against true using strict equality (===) and not type-coercing equality... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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?

🌐
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
🌐
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 …
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › javascript fundamentals
Conditional branching: if, '?'
The if (…) statement evaluates the expression in its parentheses and converts the result to a boolean. Let’s recall the conversion rules from the chapter Type Conversions: A number 0, an empty string "", null, undefined, and NaN all become false. Because of that they are called “falsy” values. Other values become true, so they are called “truthy”.
Find elsewhere
🌐
Wes Bos
wesbos.com › javascript › 07-logic-and-flow-control › if-statements-function-returns-truthy-falsy
If Statements, Function Returns, Truthy and Falsy - Wes Bos
If Statements are the foundation of all logic in JavaScript. They expect booleans, which are always either true or false, or they expect some sort of condition that is evaluated to true or false or truthy and falsy.
🌐
Happy Coding
happycoding.io › tutorials › javascript › if-statements
JavaScript Variables Tutorial
October 5, 2025 - Perhaps surprisingly, this code will work, because JavaScript coerces the strings into numbers before doing the multiplication. Similarly, if you try to use a non-boolean value in an if statement, then the code uses the “truthiness” of that value to convert it to a boolean. Values like 0, '' (empty string), and undefined are “falsy” and convert to false, and values like '42' and 'hello world' are “truthy” and convert to true.
🌐
Exploring JS
exploringjs.com › js › book › ch_booleans.html
Booleans • Exploring JavaScript (ES2025 Edition)
A value is called truthy if it is true when converted to boolean. A value is called falsy if it is false when converted to boolean. Each value is either truthy or falsy. This is an exhaustive list of falsy values: ... if (x) { // x is truthy } if (!x) { // x is falsy } if (x) { // x is truthy ...
🌐
javascript.com
javascript.com › learn › conditionals
JavaScript Conditionals: The Basics with Examples | JavaScript.com
The keyword if tells JavaScript to start the conditional statement. (10 > 5) is the condition to test, which in this case is true — 10 is greater than 5.
🌐
W3Schools
w3schools.com › jsref › jsref_if.asp
JavaScript if/else Statement
The if/else statement executes a block of code if a specified condition is true. If the condition is false, another block of code can be executed. The if/else statement is a part of JavaScript's "Conditional" Statements, which are used to perform ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › if...else
if...else - JavaScript - MDN Web Docs - Mozilla
The if...else statement executes a statement if a specified condition is truthy. If the condition is falsy, another statement in the optional else clause will be executed.
🌐
freeCodeCamp
freecodecamp.org › news › javascript-if-statement-equality-and-truthy-falsy
JavaScript if Statements, Equality and Truthy/Falsy – Explained with Examples
March 21, 2023 - The statements inside an if block are run if the decision criteria defined within the parentheses evaluate to true or truthy. The statements inside an else block are run if the decision criteria evaluate to false or falsy.
🌐
Code The Web
codetheweb.blog › if-statements-boolean-operators
All about IF statements and booleans in JavaScript! • Code The Web
February 28, 2018 - You don’t need to have the brackets, but I’ve put them in brackets just to make it a bit clearer (you are allowed to do that in JavaScript). As you can see, the code above is simply this: ... The only difference is that true has been replaced with 65 > 25 which is equivalent to true, and similarly false has been replaced with 5 + 1 == 5 which is equivalent to false. Your turn to try! See if you can complete the following tasks:
🌐
Codecademy Forums
discuss.codecademy.com › frequently asked questions › javascript faq
Why do we write `if(sale)` instead of `if(sale===true)`? - JavaScript FAQ - Codecademy Forums
April 8, 2019 - Not quite understanding why wouldn’t write If (sale === true) instead of if (sale) at the beginning. It doesn’t seem to make sense. Can anyone explain the logic there? I let sale = true; sale = false; if(sale) { console.log(‘Time to buy!’); } else { console.log (‘Time to wait for ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Logical_AND
Logical AND (&&) - JavaScript - MDN Web Docs
If expr is a function, the function is never called. See the example below: ... function A() { console.log("called A"); return false; } function B() { console.log("called B"); return true; } console.log(A() && B()); // Logs "called A" to the console due to the call for function A, // && evaluates to false (function A returns false), then false is logged to the console; // the AND operator short-circuits here and ignores function B
Top answer
1 of 3
1
I really don't understand what you are saying, but your code seems correct, but this is something that I would do javascript var btrue = true; if(btrue === true){ console.log("this is true"); }else{ console.log("this is not true"); } and it would return the string "this is true" adding new Boolean(true) seems redudant when you can simply just add true value to the variable btrue.. Also i would use three equal signs "=" to make it strictly equal to true. Hope that helps!.
2 of 3
0
conditional statement use booleans values to test for true or false statements. this is most seen in loops. including while, do while, if, if else and else. etc. for example a while loop in context would look like this. while (conditional statement =true) { //perform this code until statement evaluates to false } // so I could so something like this var counter = 0; while (counter <= 5) { console.log(' I am counting to ' + counter); counter += 1; } So I have a variable named counter with a value set to zero, Then I created a while loop that checks the conditional statement to see if that statement evaluates to true, as long as that conditional statement evaluates to true the code in the block will run. in this case the loop checks to see if the variable counter is less or equal to 5, (is 0 less than or equal 5: true) so my code block runs and string is printed to the console which would be, ' I am counting to 0 ', then my code adds 1 to the variable counter. Then the loop checks the conditional statement again, This continues until the conditional statement evaluates to false which in this case would be when the variable counter holds the value of 6 (is 6 less than or equal to 5: false) so the program ends
🌐
Udacity
udacity.com › blog › 2021 › 05 › javascript-booleans.html
Javascript Booleans: True or False Values | Udacity
September 27, 2022 - Javascript booleans are true or false values. They can be implicit ("truthy" or "falsy") or explicit, depending on a programmer's needs.