Check If A Variable Is True In JavaScript

To check if a variable is true in JavaScript, use the === (strict equality) operator. This will return true if the variable is equal to true, and false otherwise. Do not confuse true with truthy here – true is a boolean value, while truthy is any value that is not false, 0, “”, null, undefined or NaN.

check if a variable is true in javascript

For example:

var myVar = true;

if (myVar === true) {

console.log("myVar is true");

} else {

console.log("myVar is false");

}

In the above example, we use the === operator to check if myVar is equal to true. If it is, we log ‘myVar is true’ to the console. Otherwise, we log ‘myVar is false’.

The === operator considers two values of different types to be unequal, even if they are equivalent. For example, the number 1 is equivalent to the string ‘1’, but they are not equal according to ===.

See the below code for an example:

var myVar = 1;

if (myVar === "1") {

console.log("myVar is equal to 1");

} else {

console.log("myVar is not equal to 1");

}

In the code above, we use the === operator to check if myVar is equal to the string ‘1’. Because myVar is a number, and ‘1’ is a string, they are not considered equal and ‘myVar is not equal to 1’ is logged to the console.

Note that you can also use the == (equality) operator here, but it is not recommended as it can lead to unexpected results. For example, the following will return true:

var myVar = 1;

if (myVar == true) {

console.log("myVar is true");

} else {

console.log("myVar is false");

}

This is because the == operator does type coercion, meaning it will convert myVar to a boolean before checking if it is equal to true. In this case, myVar is converted to true (1 == true), and so the code logs ‘myVar is true’ to the console.

It is generally best to avoid type coercion and use the === operator instead.

Some people make a mistake and check for true value using an if conditional statement like this:

if (myVar) {

console.log("myVar is true");

} else {

console.log("myVar is false");

}

However, this will not work as expected. This is because in JavaScript, any value that is not false, 0, “”, null, undefined or NaN is considered truthy. So, the above code will actually log ‘myVar is true’ even if myVar is any of the truthy values.

For example, the following will all return true:

var myArray = [];

if (myArray) {

 console.log("true");

}

var myString = "Hello";

if (myString) {

 console.log("true");

}

var myNumber = 1;

if (myNumber) {

 console.log("true");

}

To fix this, you need to use the === operator as shown earlier.

It is always better to be explicit when writing code. This helps to avoid unexpected results and makes your code easier to understand.

Conclusion – Check If A Variable Is True In JavaScript

So, in summary, to check if a variable is true in JavaScript, use the === (strict equality) operator. This will return true if the variable is equal to true, and false otherwise.

Do not use the == (equality) operator as it can lead to unexpected results. And do not confuse true with truthy values – true is a boolean value, while truthy is any value that is not false, 0, “”, null, undefined or NaN.

Leave a Reply