if..else if Statement

if..else if Statement

  • Used when evaluation among more than two choices, use if/else if

  • You must specify a condition for every else if clause

  • You can use as many else if clauses as is needed

// if..else if statement

if (condition) {

  code to run if this condition is true

} else if (another condition) {

  code to run if this condition is true

} else {

  code to run if NONE of the above conditions are true
}

// run some other code

Example

const yourGrade = 84

if (yourGrade >= 90) {

  console.log("Congrats your score is 90 or above, that's an A!")

} else if (yourGrade >= 80) {

  console.log("Congrats your score is 80 or above, you earned a B")

} else {

  console.log("Your score is less than 80, no bueno")

}

JS Bin on jsbin.com

Exercise

  • Update the program below to test for these additional conditions:

    • If student has scored greater than or equal to 70, then print “You earned a C”

    • If student has scored greater than or equal to 60, then print “You earned a D, do better next time”

    • If the student scored below 60, then print “Unfortunately, you have failed that course will need to retake it next semester”

JS Bin on jsbin.com