JavaScript function.length counts only non-default parameters

✅ Why This Output Is NOT What Most Expect This morning’s code was: function sum(a, b, c = 10) {} console.log(sum.length); 💡 Correct Output 2 🧠 Simple Explanation (This Is the Key) In JavaScript: 👉 function.length returns the number of parameters BEFORE the first default value Let’s look at the parameters: function sum(a, b, c = 10) {} a → counted ✅ b → counted ✅ c = 10 → ❌ NOT counted So JavaScript counts only: a, b → 2 parameters That’s why: sum.length → 2 🎯 Key Takeaways (Interview Gold) function.length ≠ total parameters It counts only parameters without defaults Defaults stop the count immediately Useful for introspection & frameworks 📌 This behavior surprises many people because it’s rarely used directly. 💬 Your Turn Did you expect 2 or 3? 😄 Comment “Didn’t know this 🤯” or “Already knew 👍” #JavaScript #LearnJS #FrontendDevelopment #CodingInterview #Functions #TechWithVeera #WebDevelopment

  • graphical user interface, application

To view or add a comment, sign in

Explore content categories