JavaScript Booleans

    In JavaScript, there are two values that represent Boolean Data type true and false.

    Boolean Values

    Often in programming, we come across the situation where we want a data type that can evaluate results in the following formats.

    • Yes or No
    • On or Off
    • True or False

    In such situations, we can use a Boolean data type, that return result in true or false value.

    JS Boolean() Function

    Boolean() is a JavaScript inbuilt function that can be used to find if an expression is true or false .

    Syntax

    Boolean(expression)

    Example

    <script>
        result1 = Boolean(20>3);
        result2 = Boolean(20<3);
    
        console.log(result1)
        console.log(result2)
    </script>

    Output (console)

    true
    false

    For many cases, we even do not need to use the Boolean function we can because the comparison expression always returns a Boolean value.

    Example

    <script>
        result1 = 20>3;
        result2 = 20<3;
    
        console.log(result1);
        console.log(result2);
    </script>

    Output (console)

    true
    false

    Boolean value with comparison and condition

    When we compare two or more than two values using comparison operators we always get results in boolean values either true or false. That's the reason why comparison expressions used with conditional statements like if...else and switch case. Here are some example of comparison operator with conditional expression

    Comparison Operators Description Conditional Statement
    == Equal to if (age ==20)
    > Greater than if (age > 18)
    < Less than if (age < 18)
    <= Less than equal to if (age <=18)
    >= Greater than equal to if(age >= 18)

    Truth Value in JS

    Every value in JavaScript represents the truth value, which means every value can be represented as a true boolean value.

    Example

    Boolean(true);     //true
    Boolean("1")        //true
    Boolean("Hello")    //true
    Boolean(100)         //true
    Boolean([])             //true
    Boolean("false")     //true

    In the above example "false" is a string, not a boolean value that's why its result returns true.

    JS falsy value

    No value and 0 represent flasy value in JavaScript, which means their boolean result is false.

    Example

    Boolean(false);     //false
    Boolean(0);         //false
    Boolean("");        //false
    Boolean(null);      //false
    Boolean(undefined);     //false
    Boolean(NaN);            //false

    Boolean Objects

    By default boolean is a primitive data type, by far all the results or output we see are the primitive booleans value.

    Example

    <script>
        result1 = true;
        result2 = 1>2;
        result3 = Boolean("hello");
    
        console.log(typeof result1);
        console.log(typeof result2);
        console.log(typeof result3);
    </script>

    Output (console)

    boolean
    boolean
    boolean

    As you can see that the type of all the above results is boolean that represents a primitive data type. But JS also provides a new Boolean() constructor object that can be used to create a Boolean object.

    Example

    <script>
        result  = new Boolean(true);
    
        console.log(typeof result);
    </script>

    Output (console)

    object

    When we create a Boolean value using the new Boolean() constructor then it creates a boolean object, not a boolean primitive data type. The result would be similar for both primitive and object boolean types, but primitive data types are faster than objects.

    Summary

    • There are two boolean values in JavaScript true and false.
    • To represent a boolean value we can either use the true or false keyword or use the Boolean() function.
    • All the comparison expressions also return a boolean value.
    • Conditional statements like if...else work on boolean value.
    • Using the new Boolean() constructor we can create a boolean object.

    People are also reading: