Conditional Statements In JavaScript

Conditional Statements In JavaScript

ยท

1 min read

In JavaScript we have the following conditional statements:

  1. if

  2. else

  3. else if

  • Use if to specify a block of code to be executed, if a specified condition is true

example:

if (10 < 18) { greeting = "Good day"; }

  • Use else to specify a block of code to be executed, if the same condition is false

example:

if (10 < 18) { greeting = "Good day"; } else { greeting = "Good evening"; }

  • Use else if to specify a new condition to test, if the first condition is false

example-

if (time < 10) { greeting = "Good morning"; } else if (time < 20) { greeting = "Good day"; } else { greeting = "Good evening"; }

ย