Conditionals And Loops - Javascript

Conditionals


  • If
        if(condition) {statement}
    
  • If Else
        if(condition) {statement}
        else {statement}
    
  • If Else If
        if(condition) {statement}
        else if(condition) {statement}
        else {statement}
    
  • Ternary Operator
        condition ? {statementIfTrue} : {statementIfFalse}
    
  • Switch
        switch (variable) {
            // Variable can be String or Integer
            // Executes the statement that matches and all other statements after that
            case value1: statement1
            case value2: statement2
            default: statement
        }
    
  • break => Breaks the flow of iteration
  • continue => Moves to next iteration

Loops


  • for
        for(initialization; condition; increment) {
            // initialization runs only once
            // increment runs every time loop body is executed
        }
    
  • While
        while (condition) {
            // Statements
        }
    
  • Do-While => Statement will run at least 1 time
        do {
            // Statements
        } while(condition)
    
Share: