Tech Study

Javascript else if Statement (With Examples)

What is an if else statement in JavaScript?

If…else is a conditional statement that will execute block of code when the passed condition is truthy. When the condition is false, it will execute the else block . In if statement truthy and falsy values get converted in true and false.

Javascript else if

Many times when we write code, we want to check different conditions for different zzs. We can use conditional statements (Conditional statements are defined as performing different actions based on different conditions.) in our code to do this. In JavaScript we have the following conditional statements:

  1.  if to specify a block of code to be executed, if a specified condition is evaluated to true.
  2. else to specify a block of code to be executed if the same condition is evaluated to false
  3.  else if to specify a new condition to test, if the first condition is evaluated to false
  4.  switch to specify many different blocks of code to be executed

SYNTAX –

if (condition) {
  // code to get executed if the condition is evaluated to true
}

NOTE – if has to be in lowercase, uppercase IF, iF, If will generate error.

For Example –

if ( age < 12 )

{console.log(“Kid”)}

else if( age>12 && age <20 ){

console.log(“Teen”)}

else {

console.log(“Adult”)

}

Note that the else if statement must come after the initial if statement, and before the else statement (if there is one). You can have multiple else if statements in a sequence to test multiple conditions.

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more