Conditionals are how we branch our code based on some condition in programming languages.
Often times, the condition in a conditional is based on a comparison of some values. In javascript, we use the following operators to compare values:
== (or ===): equal to!= (or !==): not equal to>: greater than>=: greater than or equal to<: less than<=: less than or equal toConditionals are simply blocks of code wrapped in some condition statement. If this statement is true, then the code executes, otherwise it does not. They also provide a way to include alternative logic in an else block. In javascript, they look something like this:
var a = 1;
var b = 2;
if (a === b) {
// this will NOT execute...:
alert('how could this be!?!?!');
} else {
// ...but this will execute:
alert('naturally...');
}
Comparisons return booleans. We can combine booleans using the && (and) and || (or) operators. The following tables show how these operators will combine different boolean values:
&& (and) |
true | false |
|---|---|---|
| true | returns true |
returns false |
| false | returns false |
returns false |
|| (or) |
true | false |
|---|---|---|
| true | returns true |
returns true |
| false | returns true |
returns false |