Remember Booleans?

console.log(true);
console.log(false);

Comparison Operations

Javascript gives us a few operators for comparing values.

  • >=, <=, >, < inequality
  • === equality
  • !== not equal

.

log(5 > 6);
log(5 < 6);
log(5 === 6);
log(5 !== 6);
log(6 >= 6);

What do comparison operators result in?

Operations > Logical

Sometimes comparing two values is not enough. These are logical operators that are used to combine booleans.

  • ! not
  • && logical and
  • || logical or

.

log( 5 < 6 && 6 < 7 );
log('-------------------------');
log( !(2 === 3) );
log('-------------------------');
log( 2 > 3 || "red" === "red" );

Making Decisions

if statements

  • if statements are used for "branching" in code.
  • The code following the if will only be executed if the value in the parenthesis is truthy

.

var x = 5;
if (x > 3) log("x is greater than 3");

blocks

If you want to run more than one statement conditionally, use a block. That just means group them together using braces.

var x = 5;
if (x > 3) {
  log("x is greater than 3");
  log("display this too.");
}

I actually prefer always using braces, even if you're only putting one statement in your if.

else statements

The if statement has an optional else statement that is executed if the expression given to if is falsey.

var x = 5;
if (x > 3) {
    log("x is greater than 3");
}
else {
    log("x is not greater than 3");
}

Combining if/else Statements

You can combine if else statements to test several different conditions that are mutually exclusive

var isRobot = false;
var isHuman = true;
var isDog = false;

if (isRobot) {
    log("10101010101");
} 
else if (isHuman) {
    log("hello!");
}
else if (isDog) {
    log("woof woof");
}
else {
    log("...");
}

Doing the Same Thing
Over and Over

While Loops

  • A while loop is the most basic looping construct
  • It repeats a given block of code "while" a given expression is truthy.

.

var counter = 0;
while (counter < 10) {
    log(counter);
    counter++;
}

log("Final value of counter: " + counter);

While Loops

Here's another example:

var num = Math.random();
while (num < 0.7) {
    log(num);
    num = Math.random();
}

log("Greater than 0.7: " + num);

For Loops

Looping a Specific Number of Times

Note that this loop is an example of a loop that runs a specific number of times (20 in this case).

var counter = 0;
while (counter < 20) {
    // do something

    counter++; // Increment counter by 1
}

Loops like this are so common in programming, that there's a "shortcut" way of writing them.

For loop

Instead of writing:

var counter = 0;
while (counter < 20) {
    // do something

    counter++; // Increment counter by 1
}

You can use a for loop, like this:

for (var counter = 0; counter < 20; counter++) {
    // do something
}

For loop

  • For loops are used when the number of loops that need to be done can be determined
  • The for loop is made of three parts:

    for (intialization; condition; increment) {
        //code to execute
    }
    
  • First the intialization expression is executed, usually to set a loop counter

  • Next the condition is checked. If it is truthy then the code block is executed
  • Finally the increment expression is executed and then it begins again with the condition
  • Notice intiailization only happens once

For vs. While

In general:

  • Use a for loop if you know exactly how many times the loop will run (for example, counting up to 10).
  • Use a while loop if you don't know how many times the loop will run (for example, checking the result of a random number, or user input)

Truthiness and Falsiness

  • When something other than a boolean is used in a boolean context it is coerced into a boolean
  • Falsey: 0, null, undefined, "", false, NaN
  • Truthy: everything else including true and the string 'false'

.

// Falsey. Won't show.
if (0) { 
  log('0 is truthy!');
}

if (null) {
  log('null is truthy!');
}

if (undefined) {
  log('undefined is truthy!');
}

Truthiness and Falsiness

if ("") {
  log('"" is truthy!');
}

if (false) {
  log('false is truthy!');
}

if (NaN) {
  log('NaN is truthy!');
}

// Truthy. Will show.
if ("false") {
  log('"false" is truthy!');
}

if (1234) {
  log('1234 is truthy!');
}

if ("hello") {
  log('"hello" is truthy!');
}