Learn JavaScript With Me-VI

If-Else in JavaScript(JS)

Rahul Kumar Das
2 min readSep 1, 2022

Day -6 of the javascript series & today we’ll be learning about If-Else

If you are new to this series check the previous part — (link)

👦Suppose you want to perform one task or you want a block of code to run only if a specific condition is true.

🌧️Let’s say you want to go out if it doesn’t rain. Here your task is to go out and your condition is “if it doesn’t rain”

💯In the same way, if you want a block of code to run if a particular condition returns true then we go for the if statement.

❌And if you want to run another block of code if the first condition returns false, then we go for the else statement.

Let’s say we want to display this text only if a>b then:-

🚫BUT what if b>a then:-

🙋You can have recursive and also multiple if else statements in your code.

Extra

⭐ You can shorten the if-else code by using alternative syntax i.e Ternary Operation

Instead of using the above code, you can write it in:-

⭐ If it is for one line, it’s up to you to put the scope of if-else code:-

Thanks for reading 🙏🏻😇

Rahul

--

--