javascript
  1. javascript-boolean

JavaScript Boolean

Syntax

A boolean data type represents only two possible values: true or false. In JavaScript, the syntax for creating a boolean value is:

var myBoolean = true;

or

var myBoolean = false;

Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Boolean Operations Example</title>
</head>
<body>

<!-- Placeholder for displaying the output -->
<p id="output"></p>

<script>
    // Variables
    var x = 5;
    var y = 10;

    // Boolean operations
    var isGreater = x > y;
    var isLessThan = x < y;
    var isTrue = true;
    var isFalse = false;

    // Display results in the HTML document
    document.getElementById("output").innerHTML +=  isGreater + "<br>";
    document.getElementById("output").innerHTML += isLessThan + "<br>";
    document.getElementById("output").innerHTML +=  isTrue + "<br>";
    document.getElementById("output").innerHTML +=   isFalse;
</script>

</body>
</html>

Output

false
true
true
false

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Boolean Operations Example</title>
</head>
<body>

<!-- Placeholder for displaying the output -->
<p id="output"></p>

<script>
    // Variables
    var x = 5;
    var y = 10;

    // Boolean operations
    var isGreater = x > y;
    var isLessThan = x < y;
    var isTrue = true;
    var isFalse = false;

    // Display results in the HTML document
    document.getElementById("output").innerHTML +=  isGreater + "<br>";
    document.getElementById("output").innerHTML += isLessThan + "<br>";
    document.getElementById("output").innerHTML +=  isTrue + "<br>";
    document.getElementById("output").innerHTML +=   isFalse;
</script>

</body>
</html>

Try Playground

Explanation

the above example, the > operator is used to compare x and y. The result of the comparison is assigned to the isGreater variable. Since x is not greater than y, the value assigned to isGreater is false.

Similarly, the < operator is used to compare x and y. The result of the comparison is assigned to the isLessThan variable. Since x is less than y, the value assigned to isLessThan is true.

The last two lines of code demonstrate how to assign boolean values directly to variables.

Use

Booleans are often used in conditional statements to determine which code block to execute. For example:

if(isLessThan){
    console.log("x is less than y");
}else{
    console.log("x is greater than or equal to y");
}

In the above code, if the isLessThan variable is true, the code block within the first set of curly braces will be executed. Otherwise, the code block within the second set of curly braces will be executed.

Booleans can also be used in loops and for other types of logical operations.

Important Points

  • Booleans are a data type that can only have a value of true or false.
  • Booleans are often used in conditional statements, loops and other logical operations.
  • Boolean values can be assigned directly to variables.

Summary

Booleans are an important data type in JavaScript. They can be used to represent binary decisions such as true/false, yes/no, on/off, etc. They are often used in conditional statements, loops and other logical operations. Understanding how to create, assign and manipulate boolean values is an important part of learning JavaScript.

Published on: