Conditional Statements in JavaScript: If-Else

Conditional statements are used in programming to used execute specific code based on certain conditions. In JavaScript, if else execute based on the return value of the conditional statement.

The If-Else Statement

syntax:

if (condition)…


This content originally appeared on DEV Community and was authored by Max

Conditional statements are used in programming to used execute specific code based on certain conditions. In JavaScript, if else execute based on the return value of the conditional statement.

The If-Else Statement

syntax:

if (condition) {
  // execute this block of code if condition is true
} else {
  // execute this block of code if condition is false
}

Here, condition is an expression that evaluates to either true or false. If condition is true, the code inside the first set of curly braces will be executed. If condition is false, the code inside the second set of curly braces will be executed.

Example:

let score = 40;

if (age >= 40) {
  console.log("You scored above 40");
} else {
  console.log("You scored below 40");
}

In this example, if score is greater than or equal to 40, the message "You scored above 40" will be printed to the console. If score is less than 40, the message "You scored below 40" will be printed.

The If-Else If Statement

The if-else if statement can be used when there are multiple conditions that need to be evaluated.

syntax:

if (condition1) {
  // code to be executed if condition1 is true
} else if (condition2) {
  // code to be executed if condition2 is true
} else {
  // code to be executed if all conditions are false
}

Here, if condition1 is true, the code inside the first set of curly braces will be executed. If condition1 is false and condition2 is true, the code inside the second set of curly braces will be executed. If both condition1 and condition2 are false, the code inside the third set of curly braces will be executed.

Example:

let volt = 150;

if (grade >= 200) {
  console.log("Voltage above 200");
} else if (grade >= 175) {
  console.log("Voltage above 175");
} else if (grade >= 150) {
  console.log("Voltage above 150");
} else if (grade >= 100) {
  console.log("Voltage above 100");
} else {
  console.log("Voltage below 100");
}

In this example, while check the volt value in if else if statement third one is valid, Voltage above 150 will be printed in the console. Condition below the statement are valid but it won't execute because the one if statement is already valid.


This content originally appeared on DEV Community and was authored by Max


Print Share Comment Cite Upload Translate Updates
APA

Max | Sciencx (2023-04-03T15:59:18+00:00) Conditional Statements in JavaScript: If-Else. Retrieved from https://www.scien.cx/2023/04/03/conditional-statements-in-javascript-if-else/

MLA
" » Conditional Statements in JavaScript: If-Else." Max | Sciencx - Monday April 3, 2023, https://www.scien.cx/2023/04/03/conditional-statements-in-javascript-if-else/
HARVARD
Max | Sciencx Monday April 3, 2023 » Conditional Statements in JavaScript: If-Else., viewed ,<https://www.scien.cx/2023/04/03/conditional-statements-in-javascript-if-else/>
VANCOUVER
Max | Sciencx - » Conditional Statements in JavaScript: If-Else. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/04/03/conditional-statements-in-javascript-if-else/
CHICAGO
" » Conditional Statements in JavaScript: If-Else." Max | Sciencx - Accessed . https://www.scien.cx/2023/04/03/conditional-statements-in-javascript-if-else/
IEEE
" » Conditional Statements in JavaScript: If-Else." Max | Sciencx [Online]. Available: https://www.scien.cx/2023/04/03/conditional-statements-in-javascript-if-else/. [Accessed: ]
rf:citation
» Conditional Statements in JavaScript: If-Else | Max | Sciencx | https://www.scien.cx/2023/04/03/conditional-statements-in-javascript-if-else/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.