HTML Dog
Skip to navigation

Logic

A really important part of programming is being able to compare values in order to make decisions in code. When a comparison is made the outcome is either true or false; a special kind a of data called a boolean. This is logic.

Like when doing math, there’s a set of operators that work on booleans. They are used to compare two values, on the left and right of the operator, to produce a boolean value.

Equality

To find out when two values are equal, use the triple equals operator (“===”).


15.234 === 15.234

true

We can also determine if two values are not equal using the triple not equal operator (“!==”).


15.234 !== 18.4545

true

It’s important to know that strings containing a number and an actual number are not equal.


'10' === 10

false

Greater than and less than

Comparing two numbers is useful, for example, to determine which of two is larger or smaller. This first example is a comparison of 10 and 5 to see if 10 is larger, using the greater than operator (“>”).


10 > 5

true

Next we use the less than operator (“<”) to determine if the left value is smaller.


20.4 < 20.2

false

That example gives back (or returns) false, because 20.4 is not a smaller number than 20.2.

Combined comparisons

Combining a comparison of equality and size can be done with the greater than or equal to and less than or equal to operators (“>=” and “<=” respectively).


10 >= 10

true

10 <= 5

false