Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

JavaScript Essentials - Control Structures

Using conditional statements and loops in JavaScript

Control structures are used to control the flow of execution in a program. This tutorial covers the use of conditional statements and loops in JavaScript, which allow you to make decisions and repeat actions.

Key Points:

  • Conditional statements allow you to execute code based on certain conditions.
  • Loops allow you to execute a block of code multiple times.
  • Understanding control structures is essential for creating dynamic and interactive applications.

Conditional Statements

Conditional statements are used to perform different actions based on different conditions. Here are the common conditional statements in JavaScript:

  • if statement
  • else statement
  • else if statement
  • switch statement

// if statement
var age = 18;
if (age >= 18) {
    console.log('You are an adult.');
}

// if...else statement
var age = 16;
if (age >= 18) {
    console.log('You are an adult.');
} else {
    console.log('You are a minor.');
}

// if...else if...else statement
var score = 85;
if (score >= 90) {
    console.log('Grade: A');
} else if (score >= 80) {
    console.log('Grade: B');
} else {
    console.log('Grade: C');
}

// switch statement
var 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');
}
                

Loops

Loops are used to repeat a block of code multiple times. Here are the common loops in JavaScript:

  • for loop
  • while loop
  • do...while loop
  • for...in loop
  • for...of loop

// for loop
for (var i = 0; i < 5; i++) {
    console.log('Iteration ' + i);
}

// while loop
var i = 0;
while (i < 5) {
    console.log('Iteration ' + i);
    i++;
}

// do...while loop
var i = 0;
do {
    console.log('Iteration ' + i);
    i++;
} while (i < 5);

// for...in loop (used for objects)
var person = {name: 'Alice', age: 25, city: 'New York'};
for (var key in person) {
    console.log(key + ': ' + person[key]);
}

// for...of loop (used for arrays and iterable objects)
var numbers = [1, 2, 3, 4, 5];
for (var number of numbers) {
    console.log(number);
}
                

Break and Continue Statements

The break statement is used to exit a loop or switch statement, while the continue statement is used to skip the current iteration and move to the next iteration in a loop.


// break statement
for (var i = 0; i < 10; i++) {
    if (i === 5) {
        break;
    }
    console.log('Iteration ' + i);
}

// continue statement
for (var i = 0; i < 10; i++) {
    if (i === 5) {
        continue;
    }
    console.log('Iteration ' + i);
}
                

Nested Loops

Loops can be nested inside other loops to perform more complex iterations. Here is an example:


for (var i = 1; i <= 3; i++) {
    for (var j = 1; j <= 3; j++) {
        console.log('i = ' + i + ', j = ' + j);
    }
}
                

Summary

In this tutorial, you learned about control structures in JavaScript, including conditional statements and loops. These constructs allow you to control the flow of execution in your programs, making them more dynamic and interactive. Mastering these basics is essential for building more complex JavaScript applications.