Javascript is not boolean

How to check if a variable is boolean in JavaScript?

In this tutorial, we will learn to check if the variable is a Boolean in JavaScript. In JavaScript, if we match the two different variables with the same value and different data types using the equality operator, it returns a true value. It means if we compare a twovariable with the true value but different data types such as Boolean and string, it will generate a positive result.

To overcome these problems, we need to check if the variable is of type Boolean or not before matching it with other values.

Users can use the three methods below to check the variable type.

  • Using the typeof Operator
  • Using the strict equality operator (===)
  • Using the tostring.call() Method

Using the typeof Operator

The typeof operator is used to check the variable type in JavaScript. It returns the type of variable. We will compare the returned value with the “boolean” string, and if it matches, we can say that the variable type is Boolean.

Users can use the below syntax to use the typeof operator to check if the variable is of Boolean type or not.

Syntax

Parameters

Example

In the below example, we have checked the type of the different variables. Users can see the result in the output.

html> head> title>Check if variable is of boolean type/title> /head> body> h2>Check if variable is of boolean type in JavaScript using i> typeof /i> operator./h2> h4>output for value true/h4> div id = "result1">/div> h4>Output for "true"/h4> div id = "result2">/div> script> let result1 = document.getElementById("result1"); let result2 = document.getElementById("result2"); let bool = true; result1.innerHTML = typeof bool; bool = "true"; result2.innerHTML = typeof bool; /script> /body> /html>

In the above output, users can see that “true” value, it returns the type string, and for true value, typeof operator returns Boolean value.

Using the strict equality operator (===)

When we use the strict equality operator to compare the two variables, it compares both variables’ values and data types in JavaScript. As users know Boolean data type has two values which are true and false, and we will compare the variable with both Boolean values. If one of the match conditions returns true, the variable is of Boolean type.

Syntax

Follow the below syntax to check for Boolean type variable using strict equality operator.

If( variable === true || variable === false ) < // variable is of Boolean type. >

Example

In the below example, we have compared the bool name variable with the true and false Boolean values. Users can see the result in the output.

html> head> title>Check if variable is of boolean type/title> /head> body> h2>Check if variable is of boolean type in JavaScript using i> strict equality /i> operator./h2> div id = "result1">/div> script> let result1 = document.getElementById("result1"); let bool = true; if (bool === true || bool === false) result1.innerHTML = "variable bool is type of boolean."; > else result1.innerHTML = "variable bool is not type of boolean."; > /script> /body> /html>

Using the tostring.call() Method

In this section, we will learn to use the tostring.call() method to check if the variable is a type of boolean or not. It works similarly to the typeof operator but returns a different string, rather than only the variable’s data type. This method returns the string similar to ‘[object data_type]’.

Syntax

Users can follow the below syntax to use the tostring.call() method.

let result = toString.call( variable ) === '[object Boolean]'

Parameters

Example

In the below example, we have used the tostring.call() method to check the variable type and compares it with the ‘[ object boolean ]’ string. If both string matches, variable is of boolean type.

html> head> title>Check if variable is of boolean type/title> /head> body> h2>Check if variable is of boolean type in JavaScript using i> tostring.call() /i> method./h2> div id = "result1">/div> script> let result1 = document.getElementById("result1"); let bool = true; if ( toString.call(bool) === '[object Boolean]' ) result1.innerHTML = "variable bool is type of boolean."; > else result1.innerHTML = "variable bool is not type of boolean. "; > /script> /body> /html>

We have successfully learned to check if the variable is of Boolean type or not in this tutorial. In the first approach, we have used the typeof operator, which can also be used to check the data type of any variable.

In the third approach, we have used the tostring.call() method, which works the same as the type of operator. However, a strict equality operator is the best solution rather than calling the method.

Источник

Logical NOT (!)

The logical NOT ( ! ) (logical complement, negation) operator takes truth to falsity and vice versa. It is typically used with boolean (logical) values. When used with non-Boolean values, it returns false if its single operand can be converted to true ; otherwise, returns true .

Try it

Syntax

Description

Returns false if its single operand can be converted to true ; otherwise, returns true .

If a value can be converted to true , the value is so-called truthy. If a value can be converted to false , the value is so-called falsy.

Examples of expressions that can be converted to false are:

Even though the ! operator can be used with operands that are not Boolean values, it can still be considered a boolean operator since its return value can always be converted to a boolean primitive. To explicitly convert its return value (or any expression in general) to the corresponding boolean value, use a double NOT operator ( !! ) or the Boolean constructor.

Examples

Using NOT

The following code shows examples of the ! (logical NOT) operator.

!true; // !t returns false !false; // !f returns true !""; // !f returns true !"Cat"; // !t returns false 

Double NOT ( !! )

It is possible to use a couple of NOT operators in series to explicitly force the conversion of any value to the corresponding boolean primitive. The conversion is based on the «truthyness» or «falsyness» of the value (see truthy and falsy).

The same conversion can be done through the Boolean function.

!!true; // !!truthy returns true !!>; // !!truthy returns true: any object is truthy. !!new Boolean(false); // . even Boolean objects with a false .valueOf()! !!false; // !!falsy returns false !!""; // !!falsy returns false !!Boolean(false); // !!falsy returns false 

Converting between NOTs

The following operation involving booleans:

Browser compatibility

BCD tables only load in the browser

See also

Found a content problem with this page?

This page was last modified on Mar 28, 2023 by MDN contributors.

Your blueprint for a better internet.

MDN

Support

Our communities

Developers

Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.

Источник

How To Check If Type Is Boolean Using JavaScript

How to check if type is Boolean using JavaScript

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.

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)
let check = true console.log(typeof check === "boolean")

However, this approach will not work if you replace typeof with instanceof .

let check = true console.log(true instanceof Boolean)

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)

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) )
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»

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:

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

Источник

How to Check if the Type of a JavaScript Variable is Boolean?

brown beaver standing on gray rocks during daytime

Since JavaScript is a dynamically typed language, its variables can contain data of any type.

Therefore, we need a way to check for the data type of a variable.

In this article, we’ll look at ways to check if the data type of a JavaScript variable is a boolean.

The typeof Operator

One way to check if a variable is a boolean variable is using the typeof operator.

if (typeof variable === "boolean") < // . >

We check if the data of the variable variable is a boolean with the expression if the if statement.

It’ll return true if variable is a boolean.

The === Operator

Another way to check is a variable is a boolean is to check if it equals to true or false with the === operator.

For instance, we can write:

const isBoolean = (val) => < return val === false || val === true; >console.log(isBoolean(true)) console.log(isBoolean('abc')) 

We create the isBoolean that checks whether val is false or true .

Since we use the === operator, either expression in the OR expression will return true only if val is exactly equal to the value being compared.

Therefore, the first console log should log true .

And the 2nd one should log false .

Check the String Representation of the Variable with toString

In addition to checking if the value is exactly equal to true or false , we can also compare the string value of it to see if it’s a boolean.

For instance, we can write:

const isBoolean = (val) => < return val === true || val === false || toString.call(val) === '[object Boolean]'; >console.log(isBoolean(true)) console.log(isBoolean('abc')) 

We call toString.call with our val parameter to see if it returns ‘[object Boolean]’ .

If it does, then it gives us more confidence that val is a boolean.

Using the valueOf Method

In case that we have boolean variables that are boxed with the Boolean constructor, we can also call the valueOf method to return the primitive version of the boolean variable.

For instance, we can write:

const isBoolean = (val) => < return typeof val === 'boolean' || ( typeof val === 'object' && val !== null && typeof val.valueOf() === 'boolean' ); >console.log(isBoolean(true)) console.log(isBoolean('abc')) 
( typeof val === 'object' && val !== null && typeof val.valueOf() === 'boolean' ) 

to check if val is an object that isn’t null but the when we call valueOf it, we get ‘boolean’ returned.

This expression would be true if we invoke the Boolean constructor by writing expressions like:

to create a boxed boolean object.

The console logs should be the same as the previous examples.

Conclusion

There’re many ways we can use to check if a JavaScript variable is a boolean variable.

Источник

Читайте также:  Php string length utf8
Оцените статью