🥳
Black Friday Sale is on: Get Amazing deals on tools and resourcess
postImage

Tutorial: Javascript Control Flow

blogImage

By Emmanuel Chinonso

Web Developer

JavaScript If else and else if

Conditional statements are used to perform a variety of actions in response to a variety of circumstances.

Contrast Bootstrap UI Kit

Conditional Statements

When writing code, it's common to wish to perform different actions based on different decisions. To do this, you can utilize conditional statements in your code. The conditional statements below are accessible in

  • If a specific condition is true, use if to specify a block of code to be executed.
  • If the same condition is false, use else to specify a block of code to be executed.
  • If the first condition is false, use else if to define a new condition to test.
  • Use the switch to define a large number of different code blocks to run.

The if Statement

  • If a condition is true, use the if statement to specify a block of JavaScript code to run. Syntax:
1if (condition) {
2 // block of code to be executed if the condition is true
3}

It's worth noting that 'if' is written in lowercase characters. A JavaScript problem will occur if you use uppercase letters (If or IF). If the time is less than 18:00, greet them with a "Good day" greeting:

JavaScript Code:

1if (hour < 18) {
2 greeting = 'Good day';
3}

The greeting will have the following result: Good day. The else Statement If the condition is false, use the else statement to specify a block of code to run.

1if (condition) {
2 // block of code to be executed if the condition is true
3} else {
4 // block of code to be executed if the condition is false
5}

Create a "Good day" greeting if the hour is less than 18, else "Good evening":

JavaScript Code:

1if (hour < 18) {
2 greeting = 'Good day';
3} else {
4 greeting = 'Good evening';
5}

The following is the outcome of greeting:

Good day The else if Statemen If the first condition is false, use the else if statement to provide a new condition.

Syntax

1if (condition1) {
2 // block of code to be executed if condition1 is true
3} else if (condition2) {
4 // block of code to be executed if the condition1 is false and condition2 is true
5} else {
6 // block of code to be executed if the condition1 is false and condition2 is false
7}

Produce a "Good morning" greeting if the time is less than 10:00; if not, but the time is less than 20:00, create a "Good day" greeting; otherwise, a "Good evening" greeting:

JavaScript Code:

1if (time < 10) {
2 greeting = 'Good morning';
3} else if (time < 20) {
4 greeting = 'Good day';
5} else {
6 greeting = 'Good evening';
7}

The greeting will have the following result: Good day.

JavaScript Switch Statement

The switch statement is used to conduct several actions depending on certain criteria. The switch statement is used to select one of many code blocks to run.

Syntax

1switch (expression) {
2 case x:
3 // code block
4 break;
5 case y:
6 // code block
7 break;
8 default:
9 // code block
10}

This is how it works: Only one time is the switch expression evaluated. The expression's value is compared against the values in each circumstance. If a match is found, the relevant code block is run. If no match is found, the default code block is run. The weekday is returned as a number between 0 and 6 by the getDay() method. (Sunday=0, Monday=1, Tuesday=2, and so forth.) This code calculates the weekday name using the weekday number:

JavaScript Code:

1switch (new Date().getDay()) {
2 case 0:
3 day = 'Sunday';
4 break;
5 case 1:
6 day = 'Monday';
7 break;
8 case 2:
9 day = 'Tuesday';
10 break;
11 case 3:
12 day = 'Wednesday';
13 break;
14 case 4:
15 day = 'Thursday';
16 break;
17 case 5:
18 day = 'Friday';
19 break;
20 case 6:
21 day = 'Saturday';
22}

The result of day will be: Saturday

The break Keyword

JavaScript exits the switch block when it reaches the break keyword. This will bring the execution of the switch block to a halt. The last case in a switch block does not need to be broken. In any case, the block breaks (terminates) there. If you don't use the break statement, even if the evaluation doesn't match the case, the next case will be processed.

The default Keyword

If there is no case match, the default keyword indicates the code to run: The weekday is returned as a number between 0 and 6 by the getDay() method. Write a default message if today is not Saturday (6) nor Sunday (0), write a default message:

JavaScript Code:

1switch (new Date().getDay()) {
2 case 6:
3 text = 'Today is Saturday';
4 break;
5 case 0:
6 text = 'Today is Sunday';
7 break;
8 default:
9 text = 'Looking forward to the Weekend';
10}

The result of text will be: Today is Saturday

In a switch block, the default case does not have to be the last case

JavaScript Code:

1switch (new Date().getDay()) {
2 default:
3 text = 'Looking forward to the Weekend';
4 break;
5 case 6:
6 text = 'Today is Saturday';
7 break;
8 case 0:
9 text = 'Today is Sunday';
10}

Remember to terminate the default case with a break if it isn't the last case in the switch block.

Common Code Blocks

Occasionally, you'll want to utilize the same code for multiple switch scenarios. Cases 4 and 5 share the same code block, while 0 and 6 share a different code block:

JavaScript Code:

1switch (new Date().getDay()) {
2 case 4:
3 case 5:
4 text = 'Soon it is Weekend';
5 break;
6 case 0:
7 case 6:
8 text = 'It is Weekend';
9 break;
10 default:
11 text = 'Looking forward to the Weekend';
12}

Switching Details

If more than one case fits a case value, the first one is chosen. If no cases are identified that match, the program defaults to the default label. If no default label can be discovered, the program moves on to the statement(s) after the switch.

Strict Comparison

A strict comparison (===) is used in switch scenarios. In order for the values to match, they must be of the same type. Only when the operands are of the same type can a rigorous comparison be made. There won't be a match for x in this code:

JavaScript Code:

1var x = '0';
2switch (x) {
3 case 0:
4 text = 'Off';
5 break;
6 case 1:
7 text = 'On';
8 break;
9 default:
10 text = 'No value found';
11}

Contrast Bootstrap UI Kit

Create Stunning websites and web apps

Trying to build out all user interfaces and components for your website or web app from scratch can become a very tedious task. A huge reason why we created Contrast Bootstrap to help reduce the amount of time we spend doing that, so we can focus on building some other aspects of the project. Contrast Bootstrap PRO consists of a UI Kit featuring over 10000+ component variants. Together with a template of 5 admin dashboards and 23+ additional multipurpose pages template for building almost any type of website or web app. You can view a demo and learn more about Contrast by clicking here.Try contrast pro

ad-banner

Contrast Bootstrap PRO was built using the most popular CSS framework Bootstrap to help build your next landing, admin SAAS, prelaunch etc project with a clean, prebuilt and well documented template and UI components.Learn more about Contrast

Related Posts

Comments

...