JavaScript Function Parameters Linked to Arguments in Non-Strict Mode

✅ Why Changing arguments Changes the Parameter This morning’s code was: function demo(a, b) { arguments[0] = 100; console.log(a); } demo(10, 20); 💡 Correct Output 100 🧠 Simple Explanation : 🔹 Step 1: Function call demo(10, 20); a = 10 b = 20 arguments becomes: arguments = { 0: 10, 1: 20 } 🔹 Step 2: Modify arguments arguments[0] = 100; Here’s the key rule 👇 👉 In non-strict mode, parameters and arguments are linked So when you change: arguments[0] JavaScript also updates: a 🔹 Step 3: Console output console.log(a); Since a is now updated to 100, output is: 100 🎯 Key Takeaways : arguments is not an array In non-strict mode: arguments and parameters are linked Modifying arguments[index] can change parameters This does NOT happen in: strict mode arrow functions 📌 That’s why modern JavaScript prefers rest parameters: function demo(...args) {} 💬 Your Turn Did you expect 10 or 100? 😄 Comment “Tricky but clear 👍” or “Didn’t know this 🤯” #JavaScript #LearnJS #FrontendDevelopment #CodingInterview #Functions #TechWithVeera #WebDevelopment

  • graphical user interface, application, PowerPoint

To view or add a comment, sign in

Explore content categories