Logical Operators - AT THE GATES OF ENLIGHTENMENT - JavaScript in Plain Language (2015)

JavaScript in Plain Language (2015)

PART I: AT THE GATES OF ENLIGHTENMENT

1.12 Logical Operators

And: &&, Or: ||, Not: !

There are three logical operators in JavaScript.

And: &&

With && both operands need to be Boolean true in order to get a true output.

Examples:

(10 > 9) && (10 < 11); <--- The JavaScript answer is true
because true and true is true.

(10 > 14) && (10 < 11); <--- The JavaScript answer is false
because false and true is false.

5 === 7 && 5 === 5; <--- The JavaScript answer is false

5 === "5" && 5=== 5; <--- The JavaScript answer is false

5 === 5 && 7 > 5; <--- The JavaScript answer is true

Or: ||

The two vertical bars denote an OR logical operator. In US Windows you can access the || by pressing SHIFT and the last key on the right at the row starting with qwerty.

With logical or ||, one of the operands needs to be true in order to get a Boolean true as the output.

Normally JavaScript will not check the right operand in an OR statement, if the left operand is already true. This is important to know, especially when it comes to conditional decision making as we will see soon.

Examples:

(10 > 9) || (10 < 9); <--- The JavaScript answer is true because the first expression is true.

(10 < 9) || (10 > 9); <--- The JavaScript answer is true because the second expression is true.

(10 < 9) || (10 < 8); <--- The JavaScript answer is false because none of the expressions are true.

Not: !

The not ! operator is used to invert a statement. In JavaScript any data value is considered true, with the following exceptions:
Boolean false, the number zero, the value known as undefined, and the value known as null which we haven’t covered yet.

Let me declare a few variables and values in order to do some tests:

var x = 3;

var y = 0;

var z = "Tony";

var a;

Based on my first statement, is variable x true or false? It is true because it has a value. On the other hand, variable y is false.

Let me introduce a JavaScript method to find out when a value is true or false: (we can use this method in our scripts for decision making but it is not used that much. Just be aware of it):

Boolean();

And now we are going to compare the normal result of the method with the result of inverting the question. Remember, we are asking JavaScript to see if the statement is true or false, not to inspect the variable and give us a value. It’s all about our statement inside of the Boolean() method. Let’s try:

In for example, Boolean(x); We are asking the following: “Is this true?”, “I say x exists as true, is my statement true?”, and then JavaScript replies with true or false:

Boolean(x); <-- the statement is true (since our x from the previous examples is 3)

Boolean(!x); <-- the statement is false (since x has a value, !x is a false statement)

Boolean(y); <-- the statement is false (y is 0 and zero is false )

Boolean(!y); <-- the statement is true (since y is 0, !y is a true statement)

Boolean(z); <-- the statement is true (z is "Tony" and Tony is true)

Boolean(!z); <-- the statement is false (since z has a value, the !z statement is false)

Boolean(a); <-- the statement is false (a is undefined and we can’t say it is true)

Boolean(!a); <-- the a statement is true (since a is undefined, !a is a true statement)

Let’s do some lab work to review all these concepts.