The Full JavaScript & ES6 Course Notes : JavaScript Language and Essentials part3

20181016

If statement (using boolean)

Very often want to execute different pieces of code with our program depending on varying conditions.

We see 8 is greater when statement evaluated a true condition.

Because 8 is not greater than 13, so it didn’t show anything. So that covers the if statement when the condition is true.

What if we want to handle the case where the condition fails? We use the else case.

if (8>13){
console.log(“8 is greater!”)
} else{
console.log(“”8 is not greater.”)
}

There might even be times where we want a handle not as true or false values but multiple possibilities. We can handle as many conditions as we want with one if statement by adding else if clauses.

var x = 10;
if(8 > x){
console.log(“8 is greater!”)
} else if(15 > x){
console.log(“15 is greater!”)
} else{
console.log(“8 is not greater.”)
}

Now how could we easily check whether a number is odd or even?

All even numbers have a remainder of 0 when divided by 2 in all. Odd numbers have a remainder of 1 when divided by 2. Let’s use that logic now to write our help function.

  • Write a function named “check” which takes one number as a printer.

var check = function(number){
}

  • We’ll check if that number is equal to zero. And if so we’ll console.log that number is even, else we the number is odd.

if(number % 2 ==0){

console.log(number + “ is even!”);
} else{
console.log(number + “ is odd.”)
}
}

check(4);

4 is even!

check(7);

7 is odd.

Switch Statement

In Javascript code you will often see a conditional statement not use if else but use a switch keyword.

The switch statement is pretty straightforward and works very similar to if else but handles executing different blocks of code based on matching an expression.

Construct a switch statement or condition for a switch statement does not necessarily have to be a boolean value but rather it will match to an expression.

We can handle different cases of what x can match to in order to execute the different blocks of code for our switch statement.

var x = 3;

switch(x){
case 1:
console.log(“The number is one!”)
case 2:
console.log(“The number is two!”)
case 3:
console.log(“The number is three!”)
}

*Each case in a switch statement needs the break keyword in order to finish it off.

It’s like the period at the end of a sentence or so I call in at the end of a Javascript statement cases need a break to complete them. This will tell the code that a match has been found for this case and it needs to stop testing the rest of the cases.

var x = 1;

swich(x)=
{
case 1:
console.log(“The number is one!”);
break;
case 2:
console.log(“The number is two!”);
break;
case3:
console.log(“The number is three!”);
break;
}

One last component to switch statements and that’s the default case switch statements allow us to execute a default case.

console.log(“The number is” + x);
break;

If none of our code matches the cases that we we always execute at least something at a deeper case to our switch.