## Chapter 11 - Boolean Expressions

So we just talked about `if`/`then`/`else` (conditional statements). Those route code execution based on the truthiness of the values given to them. Like earlier:

``````if (position.coords) {
// some code here
}``````

We expect `position.coords` to have an object in it which in JS is truthy. This means that when it's forced to act as a boolean value (coerced into boolean), its value is turned to `true`. So theoretically, to get the same result, we could just use the boolean value `true` by itself, like this:

``````if (true) {
// some code
}``````

and the code inside its block will get executed.

So what other things yield boolean values (`true`/`false`)? Well I'm glad you've asked because it's important that you know this stuff. The answer is -- boolean expressions! Boolean expressions are expressions that yield the value of either `true` or `false`. Such expressions are the following:

``````4 == '4'   // true
4 === '4'  // false
4 != '4'   // false
4 !== '4'  // true
4 > '4'    // false
4 < '4'    // false
4 >= '4'   // true
4 <= '4'   // true``````

So here we're basically comparing two values. In all of the above examples, the first value (the one on the left) is the number four and the second value we're comparing it to (the one on the right), is the string four (because it's enclosed in quotes, it's a string).

The first boolean expression yields the boolean value `true` since JS converts (coerces) the type of our values when we use the non-strict equality operator, or the double equals (`==`), and thus make the entire expression evaluate to `true`.

Contrarily, when we use the strict equality operator (triple equals, or `===`), we just tell JS to not do any type conversion for us, and therefore, since one of our values is the number four and the other is the string four, those are not equal and thus yield the boolean value `false`.

Some more comparison operators:

`!=` simply means not equal and will convert types for you. `!==` is the strict version of it and will NOT convert types.

`4 > '4'` checks if the number four is greater than the string four and will do a conversion here as well. We will get `false` in this case, since four isn't greater than four.

And the other way around `4 < '4'` will also be `false`.

`4 >= '4'` asks if the number four is greater than or equal to the string four. This operator also uses JavaScript's conversion of types and therefore will convert the second four (the string) to the number four and yield `true` since four is equal to four.

So how can we use this stuff?

Like this:

``````function largest(num1, num2) {
if (num1 > num2) {
return num1;
} else {
return num2;
}
}``````

In our function above we simply check to find which of the numbers supplied has the largest value. If the first number (`num1`) is greater than the number in `num2` then `num1` will be returned. Otherwise we have two options: either they're equal, or `num2` is greater than `num1`. In both cases it'll be safe to return `num2`.

Note that when comparing values for equality, it is not advised to use the non-strict operators (`==` and `!=`) if you can use the strict ones instead (`===` and `!==`) in order to avoid unexpected results. For example, if we were to compare the number `0` with an empty string `""` with the non-strict operator `==`, we would get `true`. That is because JS will convert them to be the same type before doing the comparison. Had we used the strict operator, `===`, we would get `false` since no type conversion would take place. So unless you're sure of what you're doing, do this: `if (num1 === num2)` and NOT `if (num1 == num2)`, Hmmkay?

Buy on Amazon (\$9.99) 