Broad Network


ECMAScript 2015 Conditions and Boolean Logic

ECMAScript Basics - Part 6

ECMAScript 6

Foreword: In this part of the series we apply Boolean logic to ECMAScript conditions.

By: Chrysanthus Date Published: 12 May 2016

Introduction

This is part 6 of my series, ECMAScript Basics. In this part of the series we apply Boolean logic to ECMAScript conditions. You should have read the previous part of the series before coming here, as this is a continuation.

Single Expression Example
Consider the following code:

<script type="text/ECMAScript">
var me = "tall";

if (me == "tall")
    {
        alert('I am tall');
    }
</script>

Read and try the code (you have to add the surrounding HTML elements first). In the condition, (parentheses of if) there is only one expression, which is, me == "tall". If this expression results in true, the if-block will be executed. The above if-statement is equivalent to

if (true)
    {
        alert('I am tall');
    }

For this second if-statement to be executed (display alert box), you do not need the declaration of the identifier and its assignment. Read and try the following code:

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<script type="text/ECMAScript">
if (true)
    {
        alert('I am tall');
    }
</script>
</body>
</html>

The extra HTML code is to produce a web page. Let us look at a case where the condition results in false. Consider the following code:

<script type="text/ECMAScript">
var me = "short";

if (me == "tall")
    {
        alert('I am tall');
    }
</script>

The if-block (curly braces) in the above code will not be executed, because the condition results in false, since the value of the identifier, me, is “short” and not “tall”. The above if-statement is equivalent to:

if (false)
    {
        alert('I am tall');
    }

The if-block can only be executed if the condition is true. In this last case it is not executed.


More than One Expression in Condition
You can have more than one expression in a condition. In this part of the series, I consider a maximum of two expressions in a condition. Each of the expressions results in true or false. The expressions are combined with the AND, OR or NOT operators. The AND operator is typed as, &&. The OR operator is typed as, || . The NOT Operator is typed as ! . &&, || , and ! are called logical operators. With logical operators, the rules in the previous part of the series can be rewritten as:

AND
(false) && (false) = false
(false) && (true) = false
(true) && (false) = false
(true) && (true) = true

OR
(false) || (false) = false
(false) || (true) = true
(true) || (false) = true
(true) || (true) = true

NOT
!(false) = true
!(true) = false

Double-Expression Examples
The if-block will not be executed in the following code:

<script type="text/ECMAScript">
if ((false)&&(true))
    {
        alert('We are tall');
    }
</script>

A practical example for this code is:

<script type="text/ECMAScript">
var you = "tall";
var me = "tall";
if ((you == "short")&&(me == "tall"))
    {
        alert('We are tall');
    }
</script>

“tall” is assigned to the identifier, you, and also to the identifier, me. The first expression in the condition results in false and the second one results in true. (false)&&(true) gives false as the effective Boolean value for the condition. So the block is not executed.

The if-block will be executed in the following code:

<script type="text/ECMAScript">
if ((false)||(true))
    {
        alert('Either of us is tall');
    }
</script>

A practical example for this code is:

<script type="text/ECMAScript">
var you = "tall";
var me = "tall";
if ((you == "short")||(me == "tall"))
    {
        alert('Either of us is tall');
    }
</script>

Read the above code. Try it (first add the HTML parts). The first expression results in false; the second one results in true. The effective condition is true, since (false)||(true) gives true.

NOT Examples
The if-block will be executed in the following code:

<script type="text/ECMAScript">
if (!(false))
    {
        alert('I am tall');
    }
</script>

The if-block is executed, if the condition is true. !(false) gives true. If the condition is false, the if-block will not be executed.

A practical example for this code is:

<script type="text/ECMAScript">
var me = "tall";
if (!(me == "short"))
    {
        alert('I am tall');
    }
</script>

else-if and else
You can still add the else-if and else sub statements to the above code, following what we discussed in one of the previous parts of this series.

That is it for this part of the series. We stop here and continue in the next part.

Chrys

Related Links

ECMAScript Basics
ECMAScript Operators
Expressions in ECMAScript
Statements in ECMAScript
Custom Objects in ECMAScript
Functions in ECMAScript
ECMAScript Date Object
The ECMAScript String Object
ECMAScript String Regular Expressions
ECMAScript Template Literal
The ECMAScript Array
ECMAScript Sets and Maps
ECMAScript Number
Scopes in ECMAScript
Mastering the ECMAScript (JavaScript) eval Function
Sending Email with ECMAScript
ECMAScript Insecurities and Prevention
Advanced Course
Advanced ECMAScript Regular Expressions
Promise in ECMAScript 2015
Generator in ECMAScript 2015
ECMAScript Module
More Related Links
Node Mailsend
EMySQL API
Node.js Web Development Course
Major in Website Design
Low Level Programming - Writing ECMAScript Module
ECMAScript Course

BACK NEXT

Comments

Become the Writer's Follower
Send the Writer a Message