Conditional let us determine when
we do what
Using conditionals allows us to protect code from executing until the proper conditons are met.
Think of conditionals a way of putting a wall around a walled city. Your city has a gate, but you hold the power to open that gate from the right people
Let's look at an example:
var age = 18
if (age < 21) {
console.log('Sorry kid, I can't serve you')
}
In this example, we are comparing two numbers, if age
is less than (<
) 21 we excute the console.log
within the brackets. In the age the expression age < 21
evaluates
to true, so we excute the console.log
. This brings us to our next concept.
In javascript we a boolean
data types that are explictly true
or false
, but javascript assigns everything a truthy
or falsy
value. In if
blocks and ternaries
we can use truthy
and falsy
values as conditions
Let's go over some common truthy
and falsy
values in javascript.