JavaScript Switch Case and While Loops | Java Script Tutorial - Learn with VOKS
Advance AI Bootstrap C C++ Computer Vision Content Writing CSS Cyber Security Data Analysis Deep Learning Email Marketing Excel Figma HTML Java Script Machine Learning MySQLi Node JS PHP Power Bi Python Python for AI Python for Analysis React React Native SEO SMM SQL
Back

JavaScript Switch Case and While Loops

Learn this topic step-by-step with VOKS Tutorials.

Introduction To JavaScript Control Structures

In JavaScript, control structures are used to determine how your program flows — meaning which code runs and when. Two important control structures are:

  • Switch Case (used for decision making)
  • While Loops (used for repeating actions)

Switch Case In JavaScript

What Is A Switch Case?

A switch case is used when you want to check a single value against multiple possible options. It is often cleaner and easier to read than using many if...else statements.

How It Works

  • The switch takes an expression (a value).
  • It compares that value with each case.
  • When a match is found, the code inside that case runs.
  • The break keyword stops further checking.
  • If no match is found, the default case runs (optional).

Basic Syntax

switch (expression) {
    case value1:
        // code to run if expression === value1
        break;

    case value2:
        // code to run if expression === value2
        break;

    default:
        // code to run if no case matches
}

Example 1: Simple Switch Case

let day = 3;

switch (day) {
    case 1:
        console.log("Monday");
        break;

    case 2:
        console.log("Tuesday");
        break;

    case 3:
        console.log("Wednesday");
        break;

    default:
        console.log("Invalid day");
}

Output:

Wednesday

Why Break Is Important

If you forget break, JavaScript will continue executing the next cases even after a match.

Example without break:

let fruit = "apple";

switch (fruit) {
    case "apple":
        console.log("This is an apple");

    case "banana":
        console.log("This is a banana");

    default:
        console.log("Unknown fruit");
}

Output:

This is an apple
This is a banana
Unknown fruit

This behavior is called fall-through.

While Loop In JavaScript

What Is A While Loop?

A while loop is used to repeat a block of code as long as a condition remains true.

How It Works

  • The loop checks a condition.
  • If the condition is true, the code runs.
  • After running, the condition is checked again.
  • This continues until the condition becomes false.

Basic Syntax

while (condition) {
    // code to execute repeatedly
}

Example 1: Counting Numbers

let i = 1;

while (i <= 5) {
    console.log(i);
    i++;
}

Output:

1
2
3
4
5

Important Concept: Avoid Infinite Loops

If the condition never becomes false, the loop will run forever. This is called an infinite loop.

Example of an infinite loop:

let i = 1;

while (i <= 5) {
    console.log(i);
    // missing i++ causes infinite loop
}

To avoid this, always make sure the loop variable changes.

Combining Switch Case And While Loop
You can use both together to create more powerful programs.
Example: Menu System
let option = 1;

while (option <= 3) {
    switch (option) {
        case 1:
            console.log("Option 1 selected");
            break;

        case 2:
            console.log("Option 2 selected");
            break;

        case 3:
            console.log("Option 3 selected");
            break;

        default:
            console.log("Invalid option");
    }

    option++;
}

Output:

Option 1 selected
Option 2 selected
Option 3 selected
QUICK KNOWLEDGE CHECK

Test Your Understanding

Answer 5 questions generated from this lesson. Your result is calculated instantly and is not saved.

0 / 5 answered
All Courses
Advance AI Bootstrap C C++ Computer Vision Content Writing CSS Cyber Security Data Analysis Deep Learning Email Marketing Excel Figma HTML Java Script Machine Learning MySQLi Node JS PHP Power Bi Python Python for AI Python for Analysis React React Native SEO SMM SQL
Course contents
Java Script
01 Introduction 02 Application of JavaScript 03 Defining JavaScript 04 Client-Side JavaScript 05 JavaScript Syntax 06 JavaScript Varaibles 07 JavaScript Operators 08 JavaScript Statements 09 JavaScript Arrays 10 Sorting JavaScript Array 11 JavaScript Functions 12 JavaScript Objects 13 JavaScript For and For-In Loops 14 JavaScript Cookies 15 JavaScript Form Validation 16 JavaScript Error and Exception Handling 17 JavaScript Animation 18 JavaScript Switch Case and While Loops