Skip to content

JavaScript true false | Boolean Basics

  • by

The Javascript standard defines true and false values using a boolean data type. Javascript booleans may be true, false, or (in certain contexts) a value that evaluates to either true or false.

let a = true;
let b = false;

Use the reserved keywords true or false to assign a boolean value to a variable.

let str = "true";             // Incorrect: not a boolean.
let obj = new Boolean(true);  // Incorrect: overcomplicated.

Note: Don’t specify a boolean value as a string or as an object.

JavaScript true false

A simple example code demonstrates how a boolean value controls the program flow using the if condition.

<!DOCTYPE html>
<html>
<body>
  <script>
   var YES = true;
   var NO = false;

   if(YES){
    console.log("This code block will be executed");
  }

  if(NO){
    console.log("This code block will not be executed");
  }
</script>
</body>
</html>

Output:

JavaScript true false

The comparison expressions return boolean values to indicate whether the comparison is true or false.

var a = 10, b = 20;

var result = 1 > 2; // false

result = a < b; // true

result = a > b; // false

result = a + 20 > b + 5; // true

Do comment if you have any doubts or suggestions on this Js basic 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 *