DONATE  
MENU
CSS
HTML
AJAX
MYSQL
PHP
JSON
JAVASCRIPT

JavaScript Tutorials

INTRODUCTION

» Introduction to JavaScript

» Practice IT Editor

» JavaScript Example

» JavaScript Placement

» JavaScript Syntax

» JavaScript Statements

» JavaScript Input

» JavaScript Output

» JavaScript Comments

» JavaScript Variables

» Variables' Scope

» JavaScript Operators

» JS Expressions & Concatenation

» JavaScript Data Type

» JavaScript Numbers

» Number Object

» JavaScript Strings

» String Object

» JavaScript Booleans

» Boolean Object

» JavaScript Functions

» JavaScript Arrays

» Array Object - Properties

» Array Object - Methods

» JavaScript Objects

» JavaScript Undefined

» JavaScript Nulls

CONDITIONAL TESTING

» If Statement

» If Else Statement

» JavaScript Ternary Operator

» Switch Statement

LOOPS

» For Loops

» While Loops

» Break & Continue

» For...In Loops

» do...while Loop

FUNCTIONS

» JavaScript Functions

» Creating Functions

» Functions' Parameters

» Functions' Arguments

» Function Invocation

» The Return Keyword

» The Arguments Object

» Callback Functions

» Arrow Functions

ARRAYS

» JavaScript Arrays

» Creating Arrays

» Accessing Arrays

» Looping Arrays

» Multi - dimensional Arrays

OBJECTS

» JavaScript Objects

» Creating Objects

» Prototype Definition

» Accessing Objects

» The this Keyword

AJAX

» Introduction to AJAX

JSON

» Introduction to JSON

JAVASCRIPT DATES

» The Date Object

» Date Methods

» Timing (SetInterval & SetTimeout)

MATH OBJECT

» Math Object

» Math Methods

HTML / JS DOM

» DOM Introduction

» Accessing HTML Elements in DOM

» Get And Change Elements' Contents & Values

» Change Styling - DOM CSS

» DOM Attributes

» JavaScript Event Listeners

» DOM Event Listeners

» JS Events

MISCELLANEOUS

» JS Strict Mode

» JavaScript Hoisting

» JavaScript let keyword

» Reserved Keywords

» JS Best Practices

» JavaScript vs ECMAScript

JAVASCRIPT EXAMPLES

» JavaScript Calculator

.. » JavaScript » Introduction » JavaScript Booleans
Share Tweet WhatsApp

JavaScript Booleans

A JavaScript boolean is a data type that holds any one of the two possible values, true or false. Booleans are needed whenever you need a value or variable whose value can either be true or false for whatever purpose but especially in knowing the true or false state of things also known as conditional testing.
<script>
var isLogged = false;

if (isLogged == true){
alert("You are already logged in.");
}
else
{
alert("You should log in");
}
</script>

Copy PRACTICE IT

Syntax Booleans can be created explicitly. For example,
var isNum = false;
var isMale = true
NOTE:
  • true is not same as TRUE or True and false is not same as FALSE or False. The true or false must always be in lowercases.
    <script>
    alert(typeof(TRUE)); //undefined
    </script>

    Copy PRACTICE IT

You can also create booleans implicitly using the Boolean() object prototype.,
var isAdult = new Boolean(true);
It is better to create your booleans explicitly. It improves readability. Boolean Expressions These are expressions whose evaluations return final values that can either be true or false. We've understood that booleans can either be true or false but then, boolean expressions are just usual expressions whose final value can either be true or false depending on what is being evaluated. For example, 2 + 4 will return 6 after evaluation. 6 is the final value that was produced from the evaluation of the expression (2 + 4) though it was not used in the expression. So also do boolean expressions return true or false even though true or false may not be in the expressions.
<script>
alert(2 < 1); //final value is false
alert(2 == 1+1); //final value is true
</script>

Copy PRACTICE IT

Above are examples of boolean expressions. They return true or false depending on the expression. For example, 2 < 1 will not produce any other number mathematically but in JavaScript, the expression is evaluated or tested to determine if it is true or false. The testing goes thus, Is 2 less than 1? The answer is false ofcourse because 2 is not less than 1. For the second, is 2 equal to 1+1? The answer is true because 1+1 will produce 2 making both equal. The Boolean() Function The Boolean() is a function that enables you to evaluate or test if an expression is true or not. It also enables you to convert a value e.g a string to a boolean.
<script>
var a = Boolean(2 < 1); //stores false
var b = Boolean(2 == 1+1); //stores true
//RESULTS
alert(a);
alert(b);
</script>

Copy PRACTICE IT

Nevertheless, without using the Boolean() function, same result can be attain.
<script>
var a = 2 < 1; //stores false
var b = 2 == 1+1; //stores true
//RESULTS
alert(a);
alert(b);
</script>

Copy PRACTICE IT

But then, the function is useful in cases such as the one below,
<script>
var x = 0;
var y = Boolean(0);
alert(x); //0
alert(y); //false
</script>

Copy PRACTICE IT

In the above, using the function on 0 returns false because it is converting 0(a number) to a boolean.
var str = "false"; //a string
var bool = Boolean(str); //converts the string to a boolean
Some Boolean Rules These rules will guide as regard what to expect when you test variables to determine their true or false state. Knowing what to expect will enable you to write appropriate codes to execute when a condition is true and when it is false. Any Variable With A Value Returns True This means to test if a variable contains a value or not, test the variable and if it contains a value, it will return true else it will return false.
<script>
var x = 1;
var y;
alert(Boolean(x)); //true
alert(Boolean(y)); //false cause no value was assigned
</script>

Copy PRACTICE IT

<script>
var age;
if (age){
alert("Thank you for providing your age.");
}
else
{
alert("We need your age.");
}
</script>

Copy PRACTICE IT

The above will alert "We need your age." because when the variable "age" was tested with the if statement, it returned false since it was not containing a value. A Variable With Zero(0) Or -0 As Value Will Return False While a variable with "1" as value will return true. This is because 1 is same as true and 0 is same as false.
<script>
var x = 0, y = 1;
alert(Boolean(x)); //false
alert(Boolean(y)); //true
</script>

Copy PRACTICE IT

Variables With Null And Undefined As Values Will Return False
<script>
var x; //undefined
y = null; //null
alert(Boolean(x)); //false
alert(Boolean(y)); //false
</script>

Copy PRACTICE IT

An Empty String Will Return False
<script>
var str = "";
alert(Boolean(str));
</script>

Copy PRACTICE IT

NaN Will Return False A variable containing a number that is not legally acceptable in JavaScript will return false.
<script>
var num = 2 * "Two"; //NaN
alert(Boolean(num));
</script>

Copy PRACTICE IT

NOTE:
  • You don't use booleans just as used in the examples above, we usually use booleans for conditional testing using if statements. The above are just demonstrations of what will result when a variable with certain characteristics is tested in an if statement. For example, now, you know that when you test an empty string in an if statement, it will return false and execute any codes associated with the condition being false if any.
    <script>
    var str = "";
    if (str){
    alert("String is not empty");
    }
    else
    {
    alert("String is empty");
    }
    </script>

    Copy PRACTICE IT

Boolean's typeof() Using the typeof() operator on a boolean or variable containing a boolean will return boolean. Therefore, to test if a value is a boolean or not, use the typeof() operator on the value.
<script>
var bool = false;
alert(typeof(bool)); //boolean
</script>

Copy PRACTICE IT

SUMMARY
  1. A boolean is a data type whose value can either be true or false.
  2. It is used to test the state of events in a program. For example, if you are creating a calculator which should have a power button to switch it on and off. Booleans can help you determine when the calculator is on and when it is off e.g you may assign true to a variable when it is on and false when it is off.
  3. The above applies to various situations e.g trying to know if a user is logged in or not, a variable contains a value or not and etcetera.
  4. Booleans can be created explicitly and implicitly.
  5. A Boolean Expression comprise of one or more values joined together with one or more operators. It returns true or false after evaluation.
  6. There exist a function that can be use in evaluating boolean expressions. It is the Boolean() function. Its argument should be the expression to evaluate.
  7. Also listed in this page are what will result from the evaluation of some common expressions.
Take a short quiz on JavaScript Booleans.
Ask a question or tell us if JavaScript Booleans is easy or not.
cent?

Join our 1,090 subscribers to receive tutorials' updates!

  We won't spam your mail box. We hate it.





StackBeginner 2021 © Joseph Omamode