Skip to content

JavaScript Boolean if statement | Code

  • by

JavaScript Boolean if statement for any truthy value of booleanValue including true, any non-zero number, any non-empty string value, any object or array reference.

if (booleanValue)

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.

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.

if (condition) {
    // code to be executed if condition is true
} else {
    // code to be executed if condition is false

In this structure:

  • The condition is an expression that evaluates to either true or false.
  • If the condition evaluates to true, the code block inside the if statement is executed.
  • If the condition evaluates to false, the code block inside the else statement (if provided) is executed.

JavaScript Boolean if

Simple example code.

<!DOCTYPE html>
    var booleanValue = true;

    if(booleanValue === true){
      console.log("Boolean === true");




JavaScript Boolean if statement

You can also have more complex conditions using logical operators like && (AND), || (OR), and ! (NOT) to combine multiple conditions. For example:

var x = 10;
var y = 5;

if (x > 5 && y > 2) {
    console.log("Both conditions are true.");
} else {
    console.log("At least one condition is false.");

In this case, it prints “Both conditions are true.” because both conditions x > 5 and y > 2 are true.

The purpose of: “if(boolean) return;” in Javascript

return; with no value is equivalent to return undefined;. But it’s usually used in functions that aren’t expected to return a value, so it just means “exit the function now”.

So that code is equivalent to:

if (boolean) {
    return undefined;

How to check if the type is Boolean using JavaScript

Answer: Use the typeof operator to check if a value is of boolean type.

(typeof variable === 'boolean')

The typeof operator returns a string that indicates the type of a value. If the value is a boolean, the string "boolean" is returned.

const bool = true;

if (typeof bool === 'boolean') {
  console.log('✅ type is boolean');
} else {
  console.log('⛔️ type is NOT boolean');

Do comment if you have any doubts or suggestions on this Js Boolean topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading