JavaScript

Last Updated: 1/13/2023

Control Flow

  • Conditional statements are used to perform different actions based on different conditions.

Condition Statements

  • if statement
  • switch case statement

If Statement

Syntax

//variation-1 
if(condition) {
}

//variation-2
if(condition) {
}
else {
}

//variation-3
if(condition1) {
}
else if(condition2) {
}
else if(condition3) {
}
else {
}

Switch Statement

  • Used to perform different actions based on different conditions.
  • The switch expression is evaluated once.
  • The value of the expression is compared with the values of each case.
  • If there is a match, the associated block of code is executed.
  • If there is no match, the default code block is executed.

break

  • break keyword, is used to break out of the switch block.
  • If you omit the break statement, the next case will be executed even if the evaluation does not match the case.
  • It is not necessary to break the last case in a switch block. The block breaks (ends) there anyway.

Syntax

switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}

Example 1

let cardType = "gold";
switch(cardType) {
    case "silver":
        console.log("discount is 2%");
        break;
    case "gold":
        console.log("discount is 5%");
        break;
    case "platinum":
        console.log("discount is 10%");
        break;
    default:
        console.log("discount is 1%");
        break;
}

Example 2

You can put multiple cases together if they share same logic

let cardType = "gold";
switch(cardType) {
    case "silver":
        console.log("discount is 2%");
        break;
    case "gold":
    case "platinum":
        console.log("discount is 10%");
        break;
    default:
        console.log("discount is 1%");
        break;
}

Loops

  • Use Loops to execute a block of code a number of times.
  • Different kinds of loop
    • for - loops through a block of code a number of times
    • while - loops through a block of code while a specified condition is true
    • do/while - also loops through a block of code while a specified condition is true
    • for/in - loops through the properties of an object
    • for/of - loops through the values of an iterable object

For Loop

for (initial expression; condition; afterthought) {
  // code block to be executed
}

While Loop

Syntax

while (condition) {
  // code block to be executed
}

Example

let i = 0;
while(i < 10) {
    console.log(i);
    i++;
}

Do While

  • Will execute the code block once, before checking the condition is true, then it will repeat the loop as long as the condition is true.

Syntax

do {
  // code block to be executed
}
while (condition);

Example

let i = 0;
do {
   console.log(i);
   i++;
} while (i < 10)

for in

  • loops through the properties of an Object

Syntax

for (let key in object) {  
	//code block to be executed_  
}

Example

let person = {
    name: "Ganesh",
    age: 30
}
for(let key in person)
    console.log(key, person[key]);

for of

  • Loops through the values of an iterable object.

Syntax

for (let value of array) {  
	//code block to be executed_  
}

Example

let countries = ["India", "Pak", "China", "Srilanka"];

for(let item of countries)
    console.log(item);

break

  • break statement can be used to jump out of a loop
let i = 0;
while(i < 10) {
    if(i == 2) break;
    console.log(i);
    i++;
}

continue

  • continue statement jumps to next iteration
let i = 0;
while(i < 10) {
    if(i % 2 == 0){
        i++;
        continue;
    }
    console.log(i);
    i++;
}

infinite loops

  • When using loops be careful of infinite loops.
  • Forgetting to increment the variable in the loop may cause infinite loop