This site is mobile accessible. Press the "Tap Here" button to use a different font-size.


Smartphone icons created by Freepik - Flaticon

  • 6.1 JS Booleans

    A JavaScript Boolean represents one of two values: true or false.

    A JavaScript Boolean represents one of two values: true or false.

    • Very often, in programming, you will need a data type that can only have one of two values, like
      • YES / NO
      • ON / OFF
      • TRUE / FALSE
    • For this, JavaScript has a Boolean data type. It can only take the values true or false.

    The Boolean() Function

    You can use the Boolean() function to find out if an expression (or a variable) is true:

    Example 1: JS Booleans part 1
    Boolean(10 > 9)        // returns true

    Or even easier:

    Example 2: JS Booleans part 2
    (10 > 9)              // also returns true
    10 > 9                // also returns true

    Comparisons and Conditions

    • The chapter JS Comparisons gives a full overview of comparison operators.
    • The chapter JS Conditions gives a full overview of conditional statements.
    • Here are some examples:
    Operator Description Example
    == equal to if (day == "Monday")
    > greater than if (salary > 9000)
    < less than if (age < 18)

    The Boolean value of an expression is the basis for all JavaScript comparisons and conditions.

    Everything With a "Value" is True

    Example 3: JS Booleans part 3
    100
    
    3.14
    
    -15
    
    "Hello"
    
    "false"
    
    7 + 1 + 3.14

    Everything Without a "Value" is False

    The Boolean value of 0 (zero) is false:

    Example 4: JS Booleans value of 0
    var x = 0;
    Boolean(x);       // returns false

    The Boolean value of -0 (minus zero) is false:

    Example 5: JS Booleans value of -0
    var x = -0;
    Boolean(x);       // returns false

    The Boolean value of "" (empty string) is false:

    Example 6: JS Booleans value of ""
    var x = "";
    Boolean(x);       // returns false

    The Boolean value of undefined is false:

    Example 7: JS Booleans value of undefined
    var x;
    Boolean(x);       // returns false

    The Boolean value of null is false:

    Example 8: JS Booleans value of null
    var x = null;
    Boolean(x);       // returns false

    The Boolean value of false is (you guessed it) false:

    Example 9: JS Booleans value of false
    var x = false;
    Boolean(x);       // returns false

    The Boolean value of NaN is false:

    Example 10: JS Booleans value of NaN
    var x = 10 / "H";
    Boolean(x);       // returns false

    Booleans Can be Objects

    • Normally JavaScript booleans are primitive values created from literals:
    • var x = false;
    • But booleans can also be defined as objects with the keyword new:
    • var y = new Boolean(false);
    Example 11: JS Booleans and Boolean objects cannot be safely compared pt 1
    var x = false;
    var y = new Boolean(false);
    
    // typeof x returns boolean
    // typeof y returns object

    Do not create Boolean objects. It slows down execution speed. The new keyword complicates the code. This can produce some unexpected results:

    When using the == operator, equal booleans are equal:

    Example 12: JS Booleans and Boolean objects cannot be safely compared pt 2
    var x = false;             
    var y = new Boolean(false);
    
    // (x == y) is true because x and y have equal values

    When using the === operator, equal booleans are not equal, because the === operator expects equality in both type and value.

    Example 13: JS Booleans and Boolean objects cannot be safely compared pt 3
    var x = false;             
    var y = new Boolean(false);
    
    // (x === y) is false because x and y have different types

    Or even worse. Objects cannot be compared:

    Example 14: JS Booleans and Boolean objects cannot be safely compared pt 4
    var x = new Boolean(false);             
    var y = new Boolean(false);
    
    // (x == y) is false because objects cannot be compared
    • Note the difference between (x==y) and (x===y).
    • Comparing two JavaScript objects will always return false.

    Complete Boolean Reference

    Navigate this module

    Eventually the navigation links, above, will be replaced by these << (previous) and >> (next) buttons below.



    JavaScript icons used in the buttons provided by ICONS8.COM. Smartphone icons created by Freepik - Flaticon