JavaScript Default Parameters Explained

✅ Let’s Break It Down — Step by Step : This morning’s code was: function test(a = 10, b = a) { console.log(a, b); } test(5); 💡 Correct Output 5 5 🧠 Simple Explanation : 🔹 Step 1: Function call test(5); a receives the value 5 So the default a = 10 is not used 👉 Now a = 5 🔹 Step 2: Default parameter for b b = a Here’s the key rule 👇 👉 Default parameters are evaluated from left to right So when JavaScript evaluates b = a: a is already set to 5 Hence, b becomes 5 🔹 Step 3: Console output console.log(a, b); Prints: 5 5 🎯 Key Takeaways : Default parameters run left → right A default parameter can use a previous parameter If a value is passed, the default is ignored This is evaluated at function call time, not before 📌 That’s why: b = a works correctly here. 💬 Your Turn Did you expect b to be 5 or 10? 😄 Comment “Tricky but clear 👍” or “Learned something new today 🙌” #JavaScript #LearnJS #FrontendDevelopment #CodingInterview #Functions #TechWithVeera #WebDevelopment

  • graphical user interface, application

To view or add a comment, sign in

Explore content categories