Typeof operator is one of the methods to check if type is Boolean using JavaScript. In this article we will present the specific steps for you to do it. Let’s check it out!
How to check if type is Boolean using JavaScript
There are many ways to check if type is Boolean using JavaScript. It Includes built-in methods as well as comparison conditional operations. Please refer to the two ways below.
Typeof operator
Usually, in JavaScript, the typeof operator will return a string that represents the type of operand’s value.
Syntax:
typeof input
Parameters:
- input: The variable needs to check the data type.
It not only returns Boolean data type results but can also check data types of all other data types in Javascript. So in my opinion it is a perfect tool and should be used often.
If we want to manually check if the data type is Boolean, you can do like this:
let check = true
console.log(typeof check)
Output
Boolean
Or
let check = true
console.log(typeof check === "boolean")
Output
true
However, this approach will not work if you replace typeof with instanceof
.
let check = true
console.log(true instanceof Boolean)
Output
false
The instanceof operator checks whether an object is an instance of a specific class or an interface.
If the object is not properly initialized with a constructor, it returns false
.
let check = new Boolean(true)
//show the result
console.log(check instanceof Boolean)
Output
true
In addition, we can create a function that only checks for Boolean data types. Follow that method right here.
Function to check the data type
We will build a method to check if our input variable is a type of boolean as follow:
function isBoolean(input) {
return input === false || input === true;
}
//show the result
console.log(isBoolean(false) )
Or
function isBoolean (...input){
return !! input === true || !!input == false
}
//show the result
console.log(isBoolean(false) )
I create a function that takes input
as an argument. This argument is a variable that can take any variable of any data type. Inside the function, handle an effortless statement. If the parameter is true
or false
(It is compared with two true
and false
variables combined with the |
) it will return true
. If it is outside these two values, it will return false
. The operator ===
comparison means comparing data types with each other.
The function above can also be written with an arrow function.
isBoolean = (input ) => typeof input === "boolean"
Output
True
Summary
This tutorial shows you how to check if type is Boolean using JavaScript. Each approach is helpful in each case, but each method has its limitations, so be careful when using it. Let’s try this method to get your desired results! Thanks for reading!
Maybe you are interested:
- How to export multiple Classes in JavaScript
- How to check if two Sets are equal using JavaScript
- How to access the Value of a Promise in JavaScript
My name is Tom Joseph, and I work as a software engineer. I enjoy programming and passing on my experience. C, C++, JAVA, and Python are my strong programming languages that I can share with everyone. In addition, I have also developed projects using Javascript, html, css.
Job: Developer
Name of the university: UTC
Programming Languages: C, C++, Javascript, JAVA, python, html, css