javascript if conditions code example

Example 1: using if statements in javascript

if (expression) {
   Statement(s) to be executed if expression is true
} else {
   Statement(s) to be executed if expression is false
}

Example 2: else if javascript

if (condition1) {
  // code to be executed if condition1 is true
} else if (condition2) {
  // code to be executed if the condition1 is false and condition2 is true
} else {
  // code to be executed if the condition1 is false and condition2 is false
}

Example 3: JavaScript if else and else if

if (condition) {
  //  block of code to be executed if the condition is true
}
//-------------
if (hour < 18) {
  greeting = "Good day";
}
//-------------
if (condition) {
  //  block of code to be executed if the condition is true
} else {
  //  block of code to be executed if the condition is false
}
//-------------
if (hour < 18) {
  greeting = "Good day";
} else {
  greeting = "Good evening";
}
//-------------
if (time < 10) {
  greeting = "Good morning";
} else if (time < 20) {
  greeting = "Good day";
} else {
  greeting = "Good evening";
}

Example 4: if and if and else javascript

if (expression 1) {
   Statement(s) to be executed if expression 1 is true
} else if (expression 2) {
   Statement(s) to be executed if expression 2 is true
} else if (expression 3) {
   Statement(s) to be executed if expression 3 is true
} else {
   Statement(s) to be executed if no expression is true
}

Example 5: if condition javascript

if (condition){
// if true then execute this -}
 else{
 // if statment is not true then execut this -
 }

Tags:

Java Example