Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

Today, we are going to learn how we can check the boolean value in javascript if condition, here we will discuss

  1. check boolean true and false
  2. check boolean string
  3. check boolean variable

JavaScript provide check truthy value concept, where code auto handle truth values like true, "data", 1 and return false for '', false, undefined, null, 0.

Well, we will go with an example, let’s first see how we can handle if we have a boolean variable.

Handle boolean variable

const person = true;

if(person){
  console.log("True person")
} else {
  console.log("False Person")
}

when you run the above code, you will get the output true person. let’s see the output.

TypeScript, Check boolean variable example
TypeScript, Check boolean variable example

Handle boolean string

In this you have to check if(variable == 'true') like this, if match then true else false.

const person = 'false';

if(person == 'true'){
  console.log("True person")
} else {
  console.log("False Person")
}

when you run the above code, you will get the output false person. let’s see the output.

TypeScript, Check boolean string example
TypeScript, Check boolean string example

All the best đź‘Ť.