How to check if a variable is boolean in JavaScript?

JavascriptWeb DevelopmentFront End Technology

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 −

Live Demo

<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>
raja
Published on 18-Jan-2018 12:02:53
Advertisements