🛑 Read Twice Before Answering , JavaScript Thinks Faster Than We Do This looks harmless. But many answers go wrong because of how JavaScript evaluates parameters 👀 function test(a = 10, b = a) { console.log(a, b); } test(5); No tricks. No loops. No async. Still… people pause here 😄 🤔 Why this question is interesting Tests default parameters Tests evaluation order Looks beginner-friendly Still makes seniors think twice Very common interview logic 💬 Your Turn Comment your answer like this 👇 a → b → ⚠️ Please don’t run the code. Answer based on understanding. I will post the correct output + simple explanation in the evening. 📌 This post is to understand JavaScript behavior, not production patterns. #JavaScript #LearnJS #FrontendDevelopment #CodingInterview #TechWithVeera #WebDevelopment
5 5
5,5 correct answer
5 5 Dafualt value will override by the passed value so the value of a would be 5. And JS run from left to right so b will be 5.
5,5
Answer: 5 5 The argument 5 is passed to the first parameter a. Since an argument is provided, the default value of 10 for a is not used. The second parameter b has a default value of a. In JavaScript, default parameters are evaluated at call time, so b takes on the current value of a when its default is needed. At the moment the default for b is assigned, a has the value 5 (the argument passed to the function). Therefore, b also becomes 5. The console.log(a, b); statement then prints the values of the local variables a (which is 5) and b (which is 5).
5 5, but the thing is, these things used in real life programming, or just in interviews
when we call test method it's override the paremeters of passing values in function and print (5, 5) output
a=5 b=5 But it will print 55 , as no space or string in between
5 5 as argument passed is 5 default value 10 will get ignored
Any default values of the parameters are replaced by the new values. Since both a and b are 5, the output is 55. This happens because there are no spaces or blank strings between them, so the values are concatenated.