ES6 - Decision Making
ES6 - Decision Making
Section titled “ES6 - Decision Making”Decision-making constructs in programming allow a program to execute different pieces of code based on specified conditions. These structures evaluate a condition (an expression that results in true or false) before determining which block of instructions to execute.
Imagine a flowchart where a question is asked (the condition). If the answer is ‘yes’ (true), one path of actions is taken. If the answer is ‘no’ (false), a different path (or no path) is taken. This is the essence of conditional logic.
Conditional constructs in JavaScript are primarily:
| Sr.No | Statement | Description |
|---|---|---|
| 1 | if Statement | An if statement consists of a Boolean expression followed by one or more statements. The statements are executed only if the Boolean expression evaluates to true. |
| 2 | if...else Statement | An if statement can be followed by an optional else statement. The else block executes when the Boolean expression in the if statement evaluates to false. |
| 3 | else if Ladder / Nested if Statements | The else if ladder (or a series of if...else if...else statements) is useful for testing multiple conditions sequentially. It executes the block of code associated with the first condition that evaluates to true. An optional final else block can handle cases where none of the preceding conditions are met. Nested if statements involve placing an if or if...else statement inside another if or else block for more complex branching logic. |
| 4 | switch...case Statement | The switch statement evaluates an expression, matches the expression’s value against a series of case clauses, and executes statements associated with the first matching case. A break statement is typically used to exit the switch after a match. An optional default clause can execute if no case matches. |
Modern JavaScript practices, such as using let and const for variable declarations and adhering to strict equality checks (=== and !==), should be applied when writing conditional logic to ensure code clarity and prevent common errors.
Example using if...else if...else:
Section titled “Example using if...else if...else:”const score = 75;let grade;
if (score >= 90) { grade = 'A';} else if (score >= 80) { grade = 'B';} else if (score >= 70) { grade = 'C';} else if (score >= 60) { grade = 'D';} else { grade = 'F';}
console.log(`With a score of ${score}, the grade is ${grade}.`); // Output: With a score of 75, the grade is C.Example using switch:
Section titled “Example using switch:”const dayOfWeek = new Date().getDay(); // Sunday - 0, Monday - 1, ..., Saturday - 6let dayName;
switch (dayOfWeek) { case 0: dayName = 'Sunday'; break; case 1: dayName = 'Monday'; break; case 2: dayName = 'Tuesday'; break; case 3: dayName = 'Wednesday'; break; case 4: dayName = 'Thursday'; break; case 5: dayName = 'Friday'; break; case 6: dayName = 'Saturday'; break; default: dayName = 'Unknown Day';}console.log(`Today is ${dayName}.`);Understanding truthy and falsy values is also crucial in JavaScript conditional logic, as conditions don’t strictly have to be Boolean values. Values like 0, "" (empty string), null, undefined, NaN, and false itself are falsy, while most other values are truthy.