If Else, Loop and Switch 💻

If Else

Use if else whenever you want to perform task conditionally.

Syntax

if(condition){
    // task to perform
} else {
    // task to perform
}

// or

if (condition){
    // task to perform
} else if(condition){
    // task to perform
} else if(condition){
    // task to perform
} else {
    // task to perform
}

Condition can be anything like

Q. If number is even print EVEN else print ODD

let number = 10

if(number%2 === 0){
    console.log('EVEN')
} else {
    console.log('ODD')
}

Loop

Loop is the most important thing that I use most frequently in my projects to iterate over arrays.

Various types of loops that I use in development are :

  • for
    let names = ['tom', 'jerry']
    
    for(let i=0; i<2; i++){
        console.log(names[i])  // tom jerry
    }
    
  • for-in
    Gives current index on each iteration.
     let names = ['tom', 'jerry']
    
     for(let index in names){
         console.log(names[index]) // tom jerry
     }
    
  • for-of
    Gives current value on each iteration.
     let names = ['tom', 'jerry']
    
     for(let name in names){
         console.log(name) // tom jerry
     }
    
  • forEach
    built in function of array.
    forEach function returns void.
     let names = ['tom', 'jerry']
    
     names.forEach(value => {
         console.log(value)  // tom jerry
     })
    
  • map
    1. built in function of array.
    2. returns an array of values, which can be stored in variable.
    3. returns undefined if no value is returned.
    let numbers = [10, 20]
    
    let updatedNumbers = numbers.map(value => {
        return value + 10
    })
    
    console.log(updatedNumbers)     // [20, 30]
    
  • filter built in function of array.
    returns array of values.
    If condition is matched, value is added to returned array.
     let numbers = [2, 3, 4, 7]
    
     let evenNumbers = numbers.filter(value => value%2 === 0)
    
     console.log(evenNumbers)     // [2, 4]
    
     let oddNumbers = numbers.filter(value => value%2 !== 0)
    
     console.log(oddNumbers)     // [3, 7]
    

    If condition does not match for any given values of array empty array is returned
    let zeroNumbers = numbers.filter(value => value === 0)
    
    console.log(zeroNumbers)    // []
    

Switch case

Use switch case when you have more values to compare.

Syntax

switch(expression) {
  case a:
    // task to perform
    break;
  case b:
    // task to perform
    break;
  default:
    // task to perform
}
  1. expression is the value that should match the cases defined.
  2. If none of the cases matches the expression than default case runs. (Should always have a default case)
  3. If you notice the syntax you can see break keyword, that is used to simply to get out of the switch statement.
  4. If we dont use break keyword switch will move to the next case and if that case matches again it will run and execute the task to perform.

Let's understand this with the example:

let name = 'bippan'
switch (name) {
    case 'bippan':
        console.log('Developer') // Developer
        break;
    case 'sourav':
        console.log('Hacker')
        break;
    case 'rahul':
        console.log('Police')
        break;
    default:
        console.log('Enter your name')
        break;
}

Based on the name provided case is executed and it breaks out of it.

If we remove break from case 'bippan', lets see what happens.

let name = 'bippan'
switch (name) {
    case 'bippan':
        console.log('Developer')    // Developer
    case 'sourav':
        console.log('Hacker')   // Hacker
        break;
    case 'rahul':
        console.log('Police')
        break;
    default:
        console.log('Enter your name')
        break;
}

After the case bippan is executed case sourav gets executed too because case bippan does not has a break keywork.

if name is not initialized default case runs.


That's all.

BYEE.👋