Computer >> Computer tutorials >  >> Programming >> Javascript

How to check if a variable is boolean in JavaScript?


To check if a variable is Boolean in JavaScript, use the typeof operator.

Example

You can try to run the following code to check for a Boolean variable −

<html>
   <head>
      <title>JavaScript Boolean</title>
   </head>

   <body>
      <script>
         var answer = true;
         if(typeof(answer) === "boolean") {
            alert("Boolean");
         } else {
            alert("Not a Boolean");
         }
      </script>
   </body>
</html>