# JavaScript: Logic

Notes Home

## Comparison operators

Using comparison operators, we can test the values of variables to detect if they are equivalent.

``````var x = 1;
var y = 0;
var z = "1";
var a = 5;

x == y;
x != y;
x == z;

x === z;
x !== z;

x > y;
x < y;
x >= z;
x <= a;``````
• `==` two values are the same
• `!=` two values are not the same
• `>` value 1 is greater than value 2
• `<` value 1 is less than value 2
• `>=` value 1 is greater than or equal to value 2
• `<=` value 1 is less than or equal to value 2
• `===` two values are the same and same type
• `!==` two values are not the same or not the same type

We can compare strings as well as numbers.

``````var x = "hello";
var y = "world";

x == y;
x != y;``````

## Booleans

A `boolean` is a datatype that can be either `true` or `false`.

Booleans default to the values `true` and `false`.

In JavaScript other values will evaluate to `false`, such as `0`, `""` (empty string), `NaN`, `null`, and `undefined`. Everything else defaults to `true`, such as `"Hello World"`, `1`, `99`, or `-99`. Try evaluating some booleans in the console with this syntax:

``````Boolean(true);
Boolean(false);
Boolean("");
Boolean(null);
Boolean(0);
Boolean(4);``````

## Conditionals

Using booleans, we can make decision in our program with conditional statements starting with the keyword `if`:

``````var a = true;
var b = false;
if (a == true) {
console.log("This is true!");
}
if (a == false) {
console.log("This is false!");
}
if (b) {
console.log("This is true!");
}
if (!b) {
console.log("This is false!");
}
``````

We can evaluate statements using numbers and strings as well:

``````var x = 0;
var y = 1;
var a = "hello";
var b = "world";
if (x == y) { console.log("This is true!"); }
if (x > y) { console.log("This is true!"); }
if (x != y) { console.log("This is true!"); }
if (x == a) { console.log("This is true!"); }
if (x > a.length) { console.log("This is true!"); }
if (x <= a.length) { console.log("This is true!"); }``````

We can use `else` to tell our program what to do if the conditional is false:

``````var x = 0;
var y = 5;
if (x < y) {
x++;
} else {
x--;
}
console.log(x);``````

## else if and switch

Often there will be a set of conditions to test rather than a simple `if {} else {}` statement with a binary condition.

In these cases we can use `else if` to catch other conditions, or a `switch` statement.

``````
var compare = function(n1, n2) {
if (n1 == n2) {
console.log("The values are equal.");
} else if (n1 < n2) {
console.log(n1 + " is less than " + n2);
} else {
console.log(n1 + " is greater than " + n2);
}
};
compare(0, 10);
compare(1, 0);
compare(5, 5);``````
``````var x = 0;
var y = 1;
var z = 2;
switch(0) {
case x:
console.log("the value is x");
break;

case y:
console.log("the value is y");
break;

default:
console.log("does not match a value");
break;
}``````

`switch` is not used often because the pattern is easily replicated with an `object` (which we'll learn in a few weeks).

## Logical operators - AND and OR

There are a number of logical statements that help us evaluate more complex statements. We can use `&&` and `||` to add statements to our condition. `&&` requires all statements to evaluate true, reading left to right. `||` requires one of the statements on either side to be true.

``````true && false
7 == "7" && NaN != 1
false || true
7 === "7" || 1 == 1 && !(false)``````

We can also uses parathesis `()` to nest logic rules.

``( true && true || false ) && true``
``````var x = 0;
var width = 5;
var start = true;
if (x < width && start) {
x++;
} else if (x > 0) {
if (start) start = false;
x--;
}
console.log(x);
``````