DEV Community

G Gokul
G Gokul

Posted on

CONDITIONAL STATEMENTS IN JAVASCRIPT

Conditional Statements:

  • In JavaScript, conditional statements control the program flow by executing specific blocks of code based on whether a condition evaluates to true or false.
  • They are essential for decision-making in scripts, enabling dynamic and interactive behavior.
  • Conditions are evaluated using comparison and logical operators.
  • They help in building dynamic and interactive applications by responding to different inputs.

Types of Conditional Statements:

1. if Statement:

  • The if statement checks a condition written inside parentheses.
  • If the condition evaluates to true, the code inside {} is executed; otherwise, it is skipped.
  • Executes code only when a specified condition is true.
  • Useful for making simple decisions in a program.

syntax:
if (condition) {
// block of code to be executed if the condition is true
}

example:
let mark = 35;
if(mark>=35){
console.log("pass")
}

output:
pass

2. if...else Statement:

  • The if-else statement executes one block of code if a condition is true and another block if it is false.
  • It ensures that exactly one of the two code blocks runs.
  • Used when there are two possible outcomes.
  • The else block runs when the if condition is not satisfied.

syntax:
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
}

example:
let age = 17;
if(age>=18){
console.log("eligible to vote")
}else{
console.log("not eligible to vote")
}

output:
not eligible to vote

3. if...else if...else Ladder:

  • The else if statement is used to test multiple conditions in sequence.
  • It executes the first block whose condition evaluates to true.
  • Allows checking more than two conditions.
  • Evaluated from top to bottom until a true condition is found.

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

example:
let score = 75;
if (score >= 90) {
console.log("Grade: A");
} else if (score >= 75) {
console.log("Grade: B");
} else if (score >= 50) {
console.log("Grade: C");
} else {
console.log("Grade: F");
}
output:
Grade: B

4. switch Statement:

  • The switch statement evaluates an expression and executes the matching case block based on its value.
  • It provides a clean and readable way to handle multiple conditions for a single variable.
  • Used when one variable needs to be compared against multiple fixed values.
  • Improves readability compared to long if...else if chains.

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

example:
let day = 3;
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
case 4:
console.log("Thursday");
break;
case 5:
console.log("Friday");
break;
case 6:
console.log("Saturday");
break;
case 7:
console.log("Sunday");
break;
default:
console.log("Invalid day");
}

output:
Wednesday

5. Ternary Operator (? :):

  • The ternary operator is a compact shorthand for an if...else statement.
  • It is called โ€œternaryโ€ because it takes three operands:
  • A condition to test.
  • An expression to evaluate if the condition is true.
  • An expression to evaluate if the condition is false.

syntax:
(condition) ? expression1 : expression2

example:
let age = 18;
let message = (age >= 18) ? "Eligible to vote" : "Not eligible";
console.log(message);

output:
Eligible to vote

6. Nested if...else:

  • A nested if...else statement is an if...else block written inside another if or else.
  • It is used to evaluate multiple related conditions in a hierarchical manner.
  • Useful for handling complex decision-making logic.
  • Deep nesting should be avoided to maintain code readability.

nested if

example:
let tam = 37;
let eng = 45;
let mat = 67;
let sci = 56;
let soc = 77;
let total;
let avg;
if (tam>=35 && eng>=35 && mat>=35 && sci>=35 && soc>=35){
console.log("pass")
total = (tam + eng + mat + sci + soc);
console.log(total)
avg = total/5;
console.log(avg)
if (avg>=70){
console.log("grade a");
}else if(avg>=50){
console.log("grade b")
}else{
console.log("grade c")
}
}else{
console.log("fail");
}

output:
pass
282
56.4
grade b

References:

JavaScript - Conditional Statements - GeeksforGeeks

Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more.

favicon geeksforgeeks.org

https://www.w3schools.com/js/js_switch.asp

Top comments (0)