Conditional Statements - Exercise 2

The break statement exits the switch immediately after a matching case runs. Without it, execution usually falls through into later cases.

const role = "editor";
switch (role) {
  case "editor":
    console.log("Can edit");
    break;
  default:
    console.log("Read only");
}

If no case matches, JavaScript runs the default case when one is provided. Without a default case, the switch finishes without running any case body.

const color = "purple";
switch (color) {
  case "red": console.log("warm"); break;
  default: console.log("unknown color");
}

The default case is the fallback that runs when none of the other cases match the expression. It is useful for handling unexpected or unsupported values.

const status = 404;
switch (status) {
  case 200: console.log("OK"); break;
  default: console.log("Request failed");
}

You can use the ternary operator (?) in JavaScript as a concise way to write conditional statements with the syntax: condition ? expression1 : expression2. It evaluates one branch and returns that branch's value, making it useful for short assignments or return values.

const age = 20;
const label = age >= 18 ? "adult" : "minor";
console.log(label); // "adult"

You can nest ternary operators in JavaScript to handle multiple conditions, but it's recommended to use if-else statements for clarity in such cases. If you do nest them, evaluate the most specific condition first.

const score = 82;
const grade = score >= 90 ? "A" : score >= 80 ? "B" : "C";
console.log(grade); // "B"

Short-circuit evaluation in JavaScript is a behavior where the evaluation of logical expressions stops as soon as the outcome can be determined. The && operator stops on the first falsy value, while || stops on the first truthy value.

const username = "Sam";
username && console.log(`Welcome, ${username}`);

Truthy values in JavaScript are values that evaluate to true in a boolean context, while falsy values evaluate to false. The falsy values include false, 0, "", null, undefined, and NaN; most other values are truthy.

if ("hello") console.log("truthy");
if (!0) console.log("0 is falsy");

You can use logical operators (&&, ||, !) in JavaScript to perform logical operations such as AND, OR, and NOT on boolean values and expressions. They can also return an operand, which makes them useful for guards and fallback values.

const isMember = true;
const canEnter = isMember && "access granted";
console.log(canEnter);

In JavaScript, == is the equality operator that compares two values for equality, while === is the strict equality operator that not only checks for equality of values but also checks for equality of types. Prefer === in most code because it avoids conversions such as treating the string "5" as the number 5.

console.log("5" == 5);  // true
console.log("5" === 5); // false

You can use the logical AND (&&) operator in JavaScript to combine two or more conditions and check if all of them are true.

javascript

if (condition1 && condition2) {
    // code to be executed if both condition1 and condition2 are true
}