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
- check boolean
trueandfalse - check boolean string
- 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.
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.
All the best 👍.