. Advertisement .
..3..
. Advertisement .
..4..
A boolean data type returns either true or false and has two possible values. In JavaScript, the Boolean function determines whether a variable, object, condition, expression, etc., has a true or false value. Have you ever had trouble with the “How to check if type is Boolean using JavaScript” problem? If yes, let’s read this article. We will help you fix it.
What can we do to check if type is Boolean using JavaScript?
Let’s follow some solutions we give in below to check if type is Boolean using JavaScript.
Solution 1: Utilize the typeof operator
The first solution “How to check if type is Boolean using JavaScript” is utilizing the typeof operator, for example, if (typeof variable === 'boolean')
.
const bool = true;
if (typeof bool === 'boolean') {
console.log('type is boolean');
} else {
console.log('type is NOT boolean');
}
We can get the type of the value by utilizing the typeof
operator. The string "boolean"
is returned if the value is a boolean. The operator gives back a string that specifies the type of the value. Here are a few instances:
console.log(typeof true); // "boolean"
console.log(typeof false); // "boolean"
console.log(typeof function () {}); // "function"
console.log(typeof null); // "object"
console.log(typeof []); // "object"
console.log(typeof {}); // "object"
console.log(typeof ''); // "string"
console.log(typeof 0); // "number"
The typeof
operator returns the string "boolean"
when applied with a true
or false
value, which is precisely what our if
statement looks for.
const bool = true;
if (typeof bool === 'boolean') {
console.log('type is boolean');
}
The if
block is activated if the condition is pleased.
Solution 2: Compare the value of string value
We also can compare the value of string value to check whether it is a boolean. For example:
const isBoolean = (val) => {
return val === true || val === false || toString.call(val) === '[object Boolean]';
}
console.log(isBoolean(true))
console.log(isBoolean('abc'))
With val
parameter, we can call toString.call
to check if it returns "[object Boolean]"
. If so, it increases our certainty that val
is a boolean.
Solution 3: Utilize the logical OR (||) operator
Utilizing the logical OR (||) operator is also a great solution to check if type is Boolean using JavaScript.
If (variable === true || variable === false)
, for example, is used to determine whether a value is of the boolean type. Since boolean values can only be true
or false
, they have a type of boolean if one of the two conditions is true.
const bool = true;
if (bool === true || bool === false) {
console.log('type is boolean');
} else {
console.log('type is NOT boolean');
}
The logical or (||) operator was utilized to chain two criteria. The if
block executes if either condition yields a truthy value. Our conditions determine whether the value equals true
or false
. As booleans can only ever be true
or false
, the value is a boolean if either check succeeds.
Conclusion
If you’re confused with the problem “How to check if type is Boolean using JavaScript“, and the above solution is the most effective. Even if you still need help or have any questions, there is a large community to which you can turn, and everyone is usually eager to help. Finally, we wish all of our readers a wonderful day full of new ideas.
Read more
→ How to Show an Element if Checkbox is checked using JavaScript
Leave a comment