Skip to content

JavaScript Boolean, Boolean() | Basics

  • by

JavaScript has a Boolean data type and a boolean variable value is one that can either be TRUE or FALSE. The boolean function returns the boolean value of a variable.

  • The boolean primitive type that has two values of true and false.
  • The Boolean object is an object wrapper for a boolean value.
  • Use the Boolean() function to find out if an expression (or a variable) is true:

JavaScript boolean examples

Simple example code boolean primitive type. The following example declares two variables that hold boolean values of false and true:

<!DOCTYPE html>
<html>
<body>
  <script>

    let isPending = false;
    let isDone = true;

    console.log("isPending", isPending, typeof(isPending))
    console.log("isDone", isDone, typeof(isDone))

  </script>
</body>
</html>

Output:

JavaScript Boolean

Boolean object

Use the Boolean() function to convert a string into a boolean value. Because the string is not empty, it returns true.

let a = Boolean('Hi');
console.log(a); // true
console.log(typeof(a)); // boolean

The Boolean is also a wrapper object of the boolean primitive type. It means that when you pass either true or false to the Boolean constructor, it’ll create a Boolean object.

let b = new Boolean(false);
OperatorbooleanBoolean
 typeofbooleanobject
 instanceof Booleanfalsetrue

Do comment if you have any doubts or suggestions on this JS boolean topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.