We will learn about coercing non-Boolean values to Boolean values in JavaScript. For starters, the word cast is new in JavaScript. So, let’s clarify what coercion is.
Forcing is to convert a variable of one data type to another data type. As we all know, JavaScript is not a strictly typed language. Therefore, we don't need to define the type of the variable. Sometimes, JavaScript automatically coerces variables and gives unpredictable results in the output.
There are two types of casts in JavaScript. One is implicit coercion, and the other is explicit coercion. We will learn about both casts one by one in this tutorial.
An explicit cast occurs when a non-Boolean value is explicitly converted to a Boolean value using one of the Boolean coercion methods (such as Boolean() or !!).
We will discuss both methods in detail:
When we use not (!) operator on any value in JavaScript, it converts non-boolean value into boolean value.
One not(!) operator gives an error result for a value, and two not (!!) operators give an actual result for a boolean value.
Users can follow the following syntax to coerce non-Boolean values into Boolean values using the double NOT operator. -
var non_bool = "non-bool"; var bool = !!non_bool;
In the above syntax, the first not (!) operator coerces a non_bool variable of string type to a Boolean variable. The second not(!) operator is used to get the actual boolean value of the non_bool variable.
Here are some examples of using the double NOT operator -
console.log(!!0); // logs false console.log(!!1); // logs true
In the above example, the non-Boolean value 0 is explicitly coerced to a Boolean value using the double NOT operator. The double NOT operator converts its operand to a Boolean value and then inverts it, so the expression !!0 is equivalent to !(!false), which evaluates to false.
Let’s see a complete example using HTML and JavaScript
In this example, we create three numeric variables named num1, num2, and num3. Additionally, we initialized the numeric variables with different positive, negative, and zero values.
The user can observe the actual boolean value of the number in the output. The boolean value of zero is false because it is one of the false boolean values in JavaScript.
<html> <body> <h2> Coercion using <i> Doble NOT (!!) </i> Operator </h2> <p id = "output"> </p> <script> let output = document.getElementById("output"); let num1 = 10; let num2 = 0; let num3 = -10; output.innerHTML += "num1 = " + num1 + "<br/>"; output.innerHTML += "num2 = " + num2 + "<br/>"; output.innerHTML += "num3 = " + num3 + "<br/>"; output.innerHTML += "The boolean results of the above number values are given below.<br/>"; // Using Double NOT Operator num1 = !!num1; num2 = !!num2; num3 = !!num3; output.innerHTML += "num1 = " + num1 + "<br/>"; output.innerHTML += "num2 = " + num2 + "<br/>"; output.innerHTML += "num3 = " + num3 + "<br/>"; </script> </body> </html>
In JavaScript, an explicit cast is when a JavaScript developer converts a variable from one data type to another. Here we will learn to explicitly cast a non-Boolean value to a Boolean value.
We can convert any value to a boolean simply using the "boolean" constructor in JavaScript.
Users can coerce non-Boolean values in JavaScript to Boolean values by following the following syntax.
let var1 = 30; let var2 = Boolean(var1);
In the above syntax, var1 is a numeric data type and we cast it to a boolean value using the boolean constructor.
Here are some examples of using the Boolean() constructor -
console.log(Boolean(0)); // logs false console.log(Boolean(1)); // logs true
In the above example, a Boolean function is used to explicitly coerce the non-Boolean value 0 to a Boolean value. A Boolean function returns the Boolean value of its argument, so the expression Boolean(0) returns false.
When we cast any non-boolean value to a boolean value except the six false values, it always gives a true result. For the six error values, we always get the wrong boolean value as the result.
Six fake boolean values are given below.
mistake
“”
NaN
0
null
Undefined
We create different variables in this example and initialize them with different false values. We can observe that when we coerce them to boolean, it always gives a wrong boolean value.
<html> <body> <h2> Coercion using the <i> Boolean()</i> Constructor </h2> <p id = "output"> </p> <script> let output = document.getElementById("output"); // Creating the variables let falsy1 = undefined; let falsy2 = null; let falsy3 = ""; output.innerHTML += "falsy1 = " + falsy1 + "<br/>"; output.innerHTML += "falsy2 = " + falsy2 + "<br/>"; output.innerHTML += "falsy3 = " + falsy3 + "<br/>"; output.innerHTML += "The boolean results of the above falsy values are given below.<br/>"; // coercing the different falsy values to the boolean values. output.innerHTML += "falsy1 = " + Boolean(falsy1) + "<br/>"; output.innerHTML += "falsy2 = " + Boolean(falsy2) + "<br/>"; output.innerHTML += "falsy3 = " + Boolean(falsy3) + "<br/>"; </script> </body> </html>
Implicit casts occur when a non-Boolean value is used in a context where a Boolean value is expected. In this case, JavaScript will automatically convert the value to a Boolean using a set of rules called "True" and "False".
Here are some examples of how to cast non-boolean values to boolean values -
In this example, we use the hidden attribute of the image in JavaScript to display the image on click -
// Example 1: Implicit coercion in a conditional statement if (0) { console.log("This will not be logged"); } // Example 2: Implicit coercion using the logical NOT operator console.log(!0); // logs true console.log(!1); // logs false
In the first example, the value 0 is used in the conditional statement and is implicitly forced to be a boolean using the rules for true and false values. In JavaScript, the value 0 is considered false, so the conditional statement will not be executed.
In the second example, the logical NOT operator is used to negate a Boolean value that is not 0. The logical NOT operator inverts the Boolean value of its operand, so the expression !0 is equivalent to !false, which evaluates to true.
There are two types of casts in JavaScript. One is implicit coercion and the other is explicit coercion. We learned about both types of casts one by one in this tutorial.
The above is the detailed content of Explain non-boolean to boolean coercion in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!