Conditional statements in Javascript (if, else, else if and switch)

Conditional statements are very important in programming languages to do decision making tasks, according to different conditions. Javascript got many ways to providing conditional statements like if, else, else if and switch . Let us dive into different ways to write conditional statements.

If (Satisfy the condition)

If is very useful to control the decision in programming. It takes a condition inside parenthesis according to that condition’s truthiness, we will be able to run our statements (blocks of code) inside curly braces.

Let’s see how it is done…

According to above program we are able to see that when our condition of true or truthful, the statement inside if block able to run. So the use if syntax is to run particular blocks of code according to the truthfulness of the condition that we provided.

Else (if not satisfied then what else)

Else is very intuitive to understand, if the condition we provided is not true/truthful then run this block of code inside else’s curly braces. Like we were providing two cases for condition, one when condition was true another when condition was false. We have found out that when condition is true then only able to run the code inside if block, but if we wanted to run the code when condition is false we wanted to run something else. Then we can use else block to do that.

Let us assume a task that we want to print pass if marks of student is greater than 33 or else we want to print fail. For that I have provide condition inside if parenthesis over marks to check if it is greater than 33 or not, if yes then I will provide console.log(“fail”) inside if curly braces area, if that condition does not satisfy that means our logic will be pass on to the else block and it will execute the code inside it.

The code explains the logic perfectly, we have build . For marks 23 it executed the else block of code and because it does not satisfy the if condition. But for marks 67, it satisfies the if so only if block of code executed.

Else if (multiple conditions to satisfy)

Else if is very useful when we deal with multiple conditions but only one output. Let me take everyone through an example, In a class student is getting marks and we want to give him a grade. First approach to solve this would be go through multiple if conditions, but will be unable to use else statement through that. Javascript provide us with keyword else if by using that we can go through multiple condition and can produce ultimate output if any condition is not satisfied.

Let us go through problem so we can implement it.

If student gets more than 75 grade should be A, if student gets between 75 and 65 the grade should be B, If student gets between 65 and 55 the grade should be C, If student gets between 55 and 45 the grade should be D, else grade should be E.

The above is without else if, but we can see the same multiple condition solution using else if, the below syntax simplifies our problem perfectly and with that it also simplifies syntax to write multiple conditions.

Switch (multiple cases to satisfy)

Javascript introduces switch statements to solve multiple condition issue very easily. Even easier than else if, but else if has it’s own use cases, switch statement unable to solve complex condition statements like above. we can switch for equal operator cases.

Let me give you problem for that, we got a input from user a random number between 1 to 7 and we need to give back week day name on that day. For 1 output should be “Monday”, for 3 output should be “Tuesday”.

So let us start solve this problem with using else if statement…

The above code looks bit messy, switch solves that issue and give us the clean and readable syntax as a solution. Before anything else we need to understand basic syntax of switch.

In above snippet we can see that it takes expression or value to which every cases will be compared. In cases it compare the give value like x to expression and checks is x == expression if yes then run code block otherwise move onto the other given cases. after everything else it goes to default and run that code block inside default. The break cuts the propagation of comparison, if any cases satisfy the condition.

So we are ready to provide our solution for our week day problem. In our case week_number will be expression and it will satisfy the different cases in switch statements and according to that we will get our output. I did bit a tweak in below code but that is not a big change so I think everyone should be able to understand it.

I am missing ternary operators in Javscript conditions and many more shortcuts that will be discussed in another article. Thank you for reading, have a great day.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store