Navigate back to the homepage

How to check boolean value in if condition in javascript?

Infinitbility
JavaScript
May 11th, 2022 · 1 min read
keep affilitate

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

1const person = true;
2
3if(person){
4 console.log("True person")
5} else {
6 console.log("False Person")
7}

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.

1const person = 'false';
2
3if(person == 'true'){
4 console.log("True person")
5} else {
6 console.log("False Person")
7}

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 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

melio affilitate

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to check both undefined and null in typescript?

TypeScript, check both undefined and null example with step by step guide.

May 10th, 2022 · 1 min read

How to get last element of array in typescript?

TypeScript, get last element of array example with step by step guide.

May 9th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]