If/Else if and Switch Statements!
Tags: javascript, functional
Published: 2021-02-01 17:25:11

IF

In life we often do things based on certain conditions. For example, depending on certain weather conditions you may or may not walk your dog. If it is sunny you will walk your dog. If it is not sunny you will not walk your dog… or you’ll at least wear a coat! In JS there is a way to perform tasks based on certain conditions and that can be through an if/if…else statement.
An if statement is comprised of a few parts:
The key word: if
A set of parentheses () that will evaluate to either true or false
And a code block/block statement inside brackets {}
An if statement checks to see if the value inside the parentheses is equal to a Boolean “true”.
	Example)
		let sunny = true
		if (sunny) {
		console.log(‘I will walk my dog)
		{
 	Solution: I will walk my dog is logged to console

The above statement is essentially saying if sunny = true I will walk my dog. The opposite would be:
		let sunny = true
		sunny = false
		if (sunny) {
		console.log(‘I will not walk my dog)
{
	Solution: Nothing is logged to the console. It remains blank
This above statement will not log a value to the console because the value inside the parentheses is not equal to Boolean true value, it has been changed to false. If the value inside the parentheses is not explicitly equal to truthy (a value that evaluates to true) the code block will not execute. 
	

Else...If

There is however a way to have the code block execute even if the if value inside parentheses is a falsy( values that are falsy: 0, empty strings,null,undefined, NaN) and that is with an if…else statement. To continue the above set of code as an if…else statement it can be written as:
	let sunny = true
	sunny = false
	if (sunny) {
		console.log(‘I will walk my dog)
} else {
console.log(‘I will not walk my dog)
}
Solution: the above set of code is checking If sunny = true “I will walk my dog”. Else if it is not sunny (as stated in the the line of code: sunny = false), “I will not walk my dog”.
ELSE…IF chain
Else if statements  let you check for multiple different outcomes. For example, instead of just checking if the weather is sunny or rainy outside you can check if weather is cloudy, foggy, snowy, windy etc.
	let weather = 'sunny';
if (weather === 'sunny') {
 	 console.log('It\'s sunny! Where is the shade?!’);
} else if (weather === 'rainy') {
  console.log('It\'s raining! Everything is wet!’)
} else if (weather === 'cloudy') {
  console.log('It\'s cloudy! Where is the sun?!')
} else if (weather === 'windy') {
console.log('It\'s windy! We are all going to blow away')
}
else {
  console.log('Invalid season.');
}
The above set of else…if code is checking for different weather conditions and depending on the value of ‘weather’ the code will return a statement that is appropriate.
Switch
If and else if statements are good for checking a few conditions but if you had to check over 100 or 1000 conditions you would not want to write that much code. This is where switch statements come into play. They provide an easier way to write and read these kinds of statements.
The switch keyword is always followed by a set of () that contains a value after that each case will be held in brackets {}. There can be many cases. The case keyword looks to see if the expression matches value that comes next. Ex.  
Let fruit = ‘apple’
Switch (fruit)
Case ‘apple’:
	console.log(‘Apples are tasty.’);
	break;
case ‘banana’
	console.log(‘Bananas are yellow.’);
	break;
default:
	console.log(‘ Umm that’s not a fruit.)
You may notice the ‘break;’ keyword above. All that is telling the computer to do is stop checking the code or conditions after. It has found the desired condition and can stop. At the end of the cases there is always a default case in case no cases match.
Here is another example of a switch statement below.
let athleteFinalPosition = 'first place';
switch(athleteFinalPosition){
  case 'first place':
    console.log('You get the gold medal!')
    break;
  case 'second place':
    console.log('You get the silver medal!')
    break;
  case 'third place':
    console.log('You get the bronze medal!')
    break;
  default:
    console.log('No medal awarded.')
    break;
}

In conclusion if/Else...If/else and switch statements  all check given conditions and can help provide information or run functions dependent on the values of the given conditions. It is however important to keep in mind HOW MANY conditions you will be checking for. If it is one or two conditions an IF statement will be sufficient. If it is a few an if else will suffice. If there are MANY cases a switch statement may provide you a quick syntax to write your code and check all your cases. It also saves time on the processing end since the ‘Break’ keyword signals to stop checking code since conditions have been met. You may experiment with which one works best but whatever saves you the most time and effort would be what I would recommend since speed and productivity are highly valued. There are more ways to loop and check conditions of variables and arrays. A couple of other processes you could look into would be for and forEach loops.