To check if a boolean value is true
or false
in JavaScript, you can use the typeof
operator to check the data type of the value. Here's an example:
Check if Boolean is true or false in JavaScript Examples
In this example, we are defining two variables, isTrue
and isFalse
, and assigning them the values true
and false
, respectively. We are then using the typeof
operator to check the data type of each value. If the value is a boolean, then it is either true
or false
.
const isTrue = true;
const isFalse = false;
console.log(typeof isTrue); // Output: boolean
console.log(typeof isFalse); // Output: boolean
Here's an example of the output you might see from this code:
boolean
boolean
This indicates that both isTrue
and isFalse
are boolean values.
Another way to check if a boolean value is true
or false
is to use the Boolean()
function, like this:
In this example, we are using the Boolean()
function to convert the isTrue
and isFalse
values to booleans. If the value is already a boolean, then it will be returned as-is. Otherwise, it will be converted to a boolean.
const isTrue = true;
const isFalse = false;
console.log(Boolean(isTrue)); // Output: true
console.log(Boolean(isFalse)); // Output: false
The output of this code would be the same as the previous example:
true
false
which indicates that isTrue
is true
and isFalse
is false
.
Check if Boolean is true or false using if Condition Example
To check if a boolean value is true
or false
using an if
condition in JavaScript, you can simply compare the value to the true
and false
keywords, like this:
In this example, we are defining two variables, isTrue
and isFalse
, and assigning them the values true
and false
, respectively. We are then using the if
statement to compare each value to the true
and false
keywords. If the value matches, then the corresponding message is logged to the console.
const isTrue = true;
const isFalse = false;
if (isTrue === true) {
console.log('isTrue is true');
}
if (isFalse === false) {
console.log('isFalse is false');
}
Here's an example of the output you might see from this code:
isTrue is true
isFalse is false
This indicates that isTrue
is true
and isFalse
is false
.
Another way to check if a boolean value is true
or false
using an if
condition is to use the Boolean()
function, like this:
In this example, we are using the Boolean()
function to convert the isTrue
and isFalse
values to booleans. If the value is already a boolean, then it will be returned as-is. Otherwise, it will be converted to a boolean. We are then using the if
statement to check the boolean value and log a message if the condition is satisfied.
const isTrue = true;
const isFalse = false;
if (Boolean(isTrue)) {
console.log('isTrue is true');
}
if (!Boolean(isFalse)) {
console.log('isFalse is false');
}
The output of this code would be the same as the previous example:
isTrue is true
isFalse is false
which indicates that isTrue
is true
and isFalse
is false
.
Check if Boolean is true or false using switch case Statement Example
To check if a boolean value is true
or false
using a case
statement in JavaScript, you can use the switch
statement and compare the value to the true
and false
keywords, like this:
In this example, we are defining two variables, isTrue
and isFalse
, and assigning them the values true
and false
, respectively. We are then using the switch
statement to compare each value to the true
and false
keywords. If the value matches, then the corresponding message is logged to the console.
const isTrue = true;
const isFalse = false;
switch (isTrue) {
case true:
console.log('isTrue is true');
break;
default:
console.log('isTrue is not true');
break;
}
switch (isFalse) {
case false:
console.log('isFalse is false');
break;
default:
console.log('isFalse is not false');
break;
}
Here's an example of the output you might see from this code:
isTrue is true
isFalse is false
This indicates that isTrue
is true
and isFalse
is false
.
Another way to check if a boolean value is true
or false
using a case
statement is to use the Boolean()
function, like this:
In this example, we are defining two variables, isTrue
and isFalse
, and assigning them the values true
and false
, respectively. We are then using the Boolean()
function to convert each value to a boolean. We are then using the switch
statement to compare the boolean value to the true
and false
keywords. If the value matches, then the corresponding message is logged to the console.
const isTrue = true;
const isFalse = false;
switch (Boolean(isTrue)) {
case true:
console.log('isTrue is true');
break;
default:
console.log('isTrue is not true');
break;
}
switch (Boolean(isFalse)) {
case false:
console.log('isFalse is false');
break;
default:
console.log('isFalse is not false');
break;
}
Here's an example of the output you might see from this code:
isTrue is true
isFalse is false
This indicates that isTrue
is true
and isFalse
is false
.