Chapter 10 - If Statements
Chapter 10 - If Statements
🔹 Basic Example
var x = prompt("Where does the Pope live?");
if (x === "Vatican") {
alert("Correct!");
}
✔️ The prompt() asks the user a question and stores the response in x.
✔️ The if statement checks if x is exactly equal (===) to "Vatican".
✔️ If the condition is true, the alert "Correct!" is displayed.
✔️ If the user enters anything else, nothing happens.
🔹 Understanding === vs ==
=== (Strict Equality) → Checks both value and data type.
== (Loose Equality) → Converts data types if needed before comparison.
🚨 Example: == vs ===
console.log(5 == "5"); // ✅ true (Loose equality converts "5" to number)
console.log(5 === "5"); // ❌ false (Strict equality checks type too)