DEV Community

A K I L A N
A K I L A N

Posted on

IF condition (JS )

** IF statement **

  • if statement runs a block of code when a condition is true.

  • you can use if statement another if statement. - this is called nested if .

if else statement

  • else statement runs a block of code when if condition is false
if (hour < 18) {
  greeting = "Good day";
} else {
  greeting = "Good evening";
}

Enter fullscreen mode Exit fullscreen mode
  • here we have else if is use to specifie the new condition first is false.
if (time < 10) {
  greeting = "Good morning";
} else if (time < 20) {
  greeting = "Good day";
} else {
  greeting = "Good evening";
}
Enter fullscreen mode Exit fullscreen mode

*Terinary operator *

  • shorthand of if else () ? x : y

Switch statement

  • Switch statement select one or more code blocks to be executed

  • Switch is often used as a more readable alternative to many if.. else if...else statements, especially when dealing with multiple possible values

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

Enter fullscreen mode Exit fullscreen mode

break- it break out the switch block.this will stop the execution inside the switch block.

default - keyword specifies a block of code to run if there is no case match.The default keyword is optional.

Top comments (0)