Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Explain non-boolean value coercion to a boolean one in JavaScript?
We will learn non-boolean value coercion to a Boolean one in JavaScript. For beginners, coercion word is new in JavaScript. So, let's clarify what coercion is.
Coercion is converting the variable of one data type to another data type. As we all know, JavaScript is not a type-strict language. So, we don't need to define the types of the variable. Sometimes, JavaScript automatically coerces the variables and gives unpredictable results in the output.
There are two types of coercion in JavaScript. One is implicit coercion, and another is explicit coercion. We will learn both coercion one by one in this tutorial.
Falsy and Truthy Values
Before diving into coercion methods, it's important to understand which values are considered falsy in JavaScript. The six falsy values are:
false""(empty string)0(zero)NaNnullundefined
All other values are considered truthy and will coerce to true.
Explicit Coercion
Explicit coercion occurs when a non-boolean value is explicitly converted to a boolean using one of the boolean coercion methods, such as Boolean() or !!.
Using the Boolean() Constructor
The Boolean() constructor is the most straightforward way to convert any value to a boolean.
Syntax
let var1 = 30; let var2 = Boolean(var1);
Example
<html>
<body>
<h2>Coercion using the <i>Boolean()</i> Constructor</h2>
<p id="output"></p>
<script>
let output = document.getElementById("output");
// Testing various values
let values = [0, 1, "", "hello", null, undefined, [], {}];
output.innerHTML += "Boolean coercion results:<br/>";
values.forEach(value => {
output.innerHTML += `Boolean(${JSON.stringify(value)}) = ${Boolean(value)}<br/>`;
});
</script>
</body>
</html>
Using Double NOT (!!) Operator
The double NOT operator is a shorthand way to convert values to boolean. The first ! converts to boolean and inverts it, the second ! inverts it back to the correct boolean value.
Syntax
var non_bool = "non-bool"; var bool = !!non_bool;
Example
<html>
<body>
<h2>Coercion using <i>Double 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 using !! operator:<br/>";
output.innerHTML += `!!num1 = ${!!num1}<br/>`;
output.innerHTML += `!!num2 = ${!!num2}<br/>`;
output.innerHTML += `!!num3 = ${!!num3}<br/>`;
</script>
</body>
</html>
Implicit Coercion
Implicit coercion occurs when a non-boolean value is used in a context where a boolean value is expected. JavaScript automatically converts the value using truthy/falsy rules.
Common Scenarios
Implicit coercion happens in:
Conditional statements (
if,while)Logical operators (
&&,||,!)Ternary operator
Example
<html>
<body>
<h2>Implicit Boolean Coercion</h2>
<p id="output"></p>
<script>
let output = document.getElementById("output");
// Conditional statements
if (0) {
output.innerHTML += "0 is truthy<br/>";
} else {
output.innerHTML += "0 is falsy<br/>";
}
if ("hello") {
output.innerHTML += "'hello' is truthy<br/>";
}
// Logical NOT operator
output.innerHTML += `!0 = ${!0}<br/>`;
output.innerHTML += `!1 = ${!1}<br/>`;
output.innerHTML += `!"" = ${!""}<br/>`;
output.innerHTML += `!"hello" = ${!"hello"}<br/>`;
</script>
</body>
</html>
Comparison of Methods
| Method | Type | Usage | Example |
|---|---|---|---|
Boolean() |
Explicit | Constructor function | Boolean(0) // false |
!! |
Explicit | Double negation operator | !!0 // false |
if() |
Implicit | Conditional context | if(0) // falsy |
Conclusion
Boolean coercion in JavaScript follows consistent rules based on falsy and truthy values. Use Boolean() for clarity or !! for conciseness when explicit conversion is needed. Understanding both explicit and implicit coercion helps write more predictable JavaScript code.
