JavaScript Truthy vs Falsy: Understanding if Condition

✅ JavaScript Output — Let’s Understand Why This Happens This morning’s code was: let x = 0; if (x) {  console.log("Inside if"); } else {  console.log("Inside else"); } console.log(Boolean(x)); 💡 Correct Output Inside else false 🧠 Simple Explanation : 🔹 Step 1: Understanding the if condition In JavaScript, if does not check for true or false only. It checks whether a value is truthy or falsy. Here’s the key rule 👇 👉 0 is a falsy value in JavaScript. So when JavaScript evaluates: if (x) // x = 0 It treats 0 as false. That’s why the if block is skipped and the else block runs. ✔ Output: Inside else  Step 2: Boolean(x) Now we explicitly convert x into a boolean: Boolean(0) Since 0 is falsy, it becomes: false ✔ Output: false 🎯 Key Takeaways : 0 is falsy if (x) checks truthy / falsy, not strict booleans Boolean(value) shows how JavaScript internally treats a value 📌 Common falsy values in JS: javascript Copy code false, 0, "", null, undefined, NaN 💬 Your Turn Did you already know 0 is falsy? Comment “Knew it 👍” or “Learned today 😮” #JavaScript #FrontendDevelopment #LearnJS #CodingInterview #TruthyFalsy #TechWithVeera #WebDevelopment #100DaysOfCode

  • graphical user interface, application

Finally after so long i told correct answer .

Like
Reply

To view or add a comment, sign in

Explore content categories