Day 15: Treehouse Full Stack JavaScript Techdegree

Today I learned more about JavaScript’s conditionals. I am actually quite familiar with this lesson. I have practiced and applied a lot of JavaScript’s conditionals in some of my projects though this was definitely helpful because I picked up new tips and tricks with how to approach different situations. Definitely, my skills with this topic expanded since I learned new things.

These are my notes:

Branches

Flow of execution based on the condition

Unlike if-statements, all cases are contained in the code block

Using switch ( ifstatement ) {

case: ___

Run this code

break;

}

We are basically saying if the if statement is true in the case, you run that case’s code and break/exit the entire switch statement

As you can see it entirely skips the first case and runs the second case because the day variable is = 1 and not 0.

Now the program has 2 branches. Each case represents a branch. Similarly to else-if statements being a multibranch.

If you remove the break statement — the statement will fall through to the next code

You need to add the break statement to exit the case/the branch once the branch executes.

We can add as many cases as we want.

Default option: which is basically going to be the else statement in the code block. If the value is not true in any of the cases, default runs.

Ternary Operator: Compact, two-way branching conditional expression : only use this for simple code execution

<boolean> ? <expression if true> : <expression if false>

If the boolean is true, then the code after the ? will run. If not then : will run.

You can even assign the result of a ternary operator inside a variable and call the variable containing the returned value from the ternary operator

Short Circuit Evaluation: Stopping code execution as soon as possible

Using && operator

If the first value is truthy it returns the second value after the && operator

Now it returns MIGUEL because all operands are truthy

Now lets make the middle false

The interpreter moves from left to right

If any of the operands are falsy then the others are not considered

So the interpreter never considers the string ‘MIGUEL’ because the value before is already falsy

This allows us to check a condition and only run if it returns true

|| or operand

Returns the first value it finds true from left to right

Now it returns renzo because the first or operand returns false

We can set a default value even if the function is called without an argument. We can use the || operand to do that

If start was not given as an argument by the function it will be return as false so the || operand will initiate and then start will be 1

Or assign the default value in the parameter

Or

It returns the console.log because the first && operand is true