JavaScript's Boolean function?

The Boolean() function in JavaScript converts any value to its boolean equivalent (true or false). This is useful for conditional checks and validation in your applications.

Syntax

Boolean(value);

It takes any value or expression and returns true for truthy values and false for falsy values.

Falsy Values

These values always return false when converted to boolean:

<html>
<body>
<script>
   console.log(Boolean(0));           // false
   console.log(Boolean(""));          // false (empty string)
   console.log(Boolean(null));        // false
   console.log(Boolean(undefined));   // false
   console.log(Boolean(NaN));         // false
   console.log(Boolean(false));       // false
</script>
</body>
</html>

Truthy Values

All other values return true, including non-empty strings, numbers (except 0), and objects:

<html>
<body>
<p id="boolean"></p>
<script>
   var a = 0;
   document.write(Boolean(a));        // displays false
   var b = 1;
   document.write(Boolean(b));        // displays true
   
   var x = Boolean(100);
   var y = Boolean("Hello");
   var z = Boolean('false');          // Note: string 'false' is truthy!
   
   document.getElementById("boolean").innerHTML =
   "100 is " + x + "<br>" +
   "string 'Hello' is " + y + "<br>" +
   "string 'false' is " + z;
</script>
</body>
</html>
falsetrue100 is true
string 'Hello' is true
string 'false' is true

Using Boolean with Expressions

You can pass expressions to Boolean() to evaluate their truth value:

<html>
<body>
<script>
   document.write(Boolean(10 > 5));   // true
   document.write("<br>");
   document.write(Boolean(1 > 4));    // false
   document.write("<br>");
   document.write(Boolean("hello" && "world"));  // true
   document.write("<br>");
   document.write(Boolean("" || 0));  // false
</script>
</body>
</html>
true
false
true
false

Common Use Cases

The Boolean() function is commonly used for:

  • Converting user input to boolean values
  • Validating form fields
  • Checking if variables have meaningful values
  • Converting API responses to boolean format

Comparison with Double Negation

Method Example Result
Boolean() Boolean("hello") true
Double negation !!"hello" true

Conclusion

The Boolean() function is essential for explicit boolean conversion in JavaScript. Remember that only six values are falsy: 0, "", null, undefined, NaN, and false.

Updated on: 2026-03-15T23:18:59+05:30

227 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements