JavaScript for beginners: Conditions in JavaScript

2018-07-29
Carton image to show conditions as an introduction to JavaScript

As technology moves to the web and as web apps and mobile apps continue to rise in popularity, web servers have become increasingly important. The demand for people who can program JavaScript has increased as well. This article continues to explore the different programming concepts in JavaScript. Today we’ll focus on conditional statements in JavaScript.

What are conditional statements?

Conditional statements are used to execute actions based on the occurrence or the absence of specific conditions. It is common to think before you write your application’s code that you may want to implement a certain action only under certain circumstances. Conditional statements are here to come to the rescue.


JavaScript has two types of conditional statements: if statements and switch statements. The if statement executes a block of code only if the condition you specify is true. You can add on the else keyword to execute another block of code if the first condition was false.


The code snipping below shows the syntax for an if statement. Make sure you add the proper curly brackets and parenthases, and don’t forget the semicolons!

Example:

if (the condition to be checked) {

code executed in case of condition occurrence

}

else {

code executed in case of condition is absent

}

code executed in case of condition occurrence

}

else {

code executed in case of condition is absent

}

Another useful addition to if statements is the else if command. This command gives you more control since you can specify that multiple conditions must be checked to see whether they are true. As soon as the computer finds one condition that is true, it will skip the rest of the if statement.

The Switch:

You can replace multiple if/elseif/else statements with a single control statement called the switch statement, which is used to execute multiple actions based on different conditions. The switch expression lists a condition between parenthesis. The condition is compared to all the cases within the statement, and when a correct match is found the corresponding code block is executed.

Example:

switch (number) {

case 0:

code block 1;

break;

case 1:

code block 2;

break;

case 2:

code block 3;

}


The break keyword is treated as a sign to exit the switch statement immediately. We add it after every case to save time by stopping the switch statement once the correct case is found.

Default case:

The keyword is a keyword used as a fallback if there is no match between the condition and any of the cases. Keep in mind that it does not necessarily have to be the last case in a switch block.


Get excited about these coding concepts! You can try them out on your own and soon you’ll be an excellent programmer. Start your learning journey today with RoboGarden. Register for free.


JavaScript Series Introduction to JavaScript

RELATED BLOGS

Start JavaScript coding

Let's start JavaScript coding with RoboGarden learning journey.

Got an invite