﻿ ﻿Comparison operators and booleans - AT THE GATES OF ENLIGHTENMENT - JavaScript in Plain Language (2015)

# JavaScript in Plain Language (2015)

## PART I: AT THE GATES OF ENLIGHTENMENT

### 1.9 Comparison operators and booleans

===, !==, <, >, <=, >= true, false

In math we use the equals sign to determine equality.

Not in JavaScript! In JavaScript the equals sign is an assignment operator: it assigns the right operand to the left operand:

x = 12;

In the example above, 12 is given to x. This is an assignment, not a question to inspect equality.

To inspect equality, we use the triple = operator ===

x === 12;

The expression above is not an assignment, it is a question and the question is: “are the values in x and in 12 the same?” in other words, does the variable x contain the numeric value 12?

Every time JavaScript interpreter sees a triple equals (===), it always evaluates and replies with a Boolean true or a Boolean false.

What is a Boolean?

A Boolean is a binary evaluation result that has one of two possible value outcomes:
true or false. In JavaScript a Boolean is a type of value just like string, number, undefined, null, etc.

true and false are permanently reserved words in JavaScript. We can literally write true and false without quotes because JavaScript knows what they mean. However, if we ever write "true" or "false" in quotes, JavaScript will assume it is a string which has no value as a Boolean. So if you ever see "true" in quotes, it is because that information is being spoken to a human being, not to the computer itself. JavaScript only accepts true and false as Boolean values when the terms are not wrapped in quotes and in lower case.

So, we could create a question on the console for JavaScript to answer (this question is a command to get a Boolean result):

x === 12;

If we have previously declared x with the value of 12, JavaScript will reply true because the statement is true.

Else, it will reply false, like in the following example:

x === 14;

Question:

In x === 14 what would JavaScript say if x was not previously declared as a variable?

Since x is not wrapped in quotes, it is not a string value and therefore it must be a variable name. However JavaScript doesn’t find it in its library because it hasn’t been declared.

In that case, instead of replying with a Boolean true or false, JavaScript will throw an error like this:

ReferenceError: x is not defined

Let’s learn a few more comparison operators and ask more questions to JavaScript.

Please remember, these symbols compare the left operand, to the right operand.

Less than: <

Greater than: >

Less or equals than: <=

Greater or equals than >=

Not the same as: !==

Examples:

12 < 10; The Boolean answer is false

12 > 10; The Boolean answer is true

12 === 12; The Boolean answer is true

12 === "12"; The Boolean answer is false*

12 !== 13; The Boolean answer is true

12 !== 12; The Boolean answer is false

12 <= 12; The Boolean answer is true

12 = 12; The Boolean answer is true

*Note: You may have seen double equals == used in JavaScript in another book somewhere. In a different programming language that should be ok. However, in JavaScript the double equals comparison operator is not recommended. This is because in JavaScript the double equals == does not fully check the condition. I mean it does not check the type of data the operands contain. That could lead to errors because sometimes we have numbers in quotes which makes them strings, a "12" is not the same value as 12. The first is a string of characters and the second is a number.

In JavaScript always use the triple equals: === for comparing values.

﻿