🚨 JavaScript Interview Question What will be the output? 🤔 function greet(name) { if (name === undefined) { console.log("Hello, guest!"); } else { console.log("Hello, " + name); } } greet(); greet("Amrutha"); greet("Vineeth", "How are you?"); Looks simple… but there’s a twist 👀 👉 What will be the output? 👉 Why does the last call behave differently? Bonus: How does JavaScript handle extra arguments? 🔥 #JavaScript #FrontendInterview #WebDevelopment #CodingInterview #ProductBasedCompany
greet(); -> Hello, guest greet("Amrutha"); -> Hello, Amrutha greet("Vineeth", "How are you?"); -> Hello Vineeth
Hello Guest Hello Amrutha Hello Vineeth
hello AMRUTHA , HELLO VINEETH HOW ARE YOU
1. It will show error as it accept a argument 2.Hello Amrutha 3.error as it accept only one argument not two
Hello, guest Hello, Amrutha Hello, Vineeth I know this is correct and I expected this
As expected, in the last call we passed two arguments, but the function only uses the first argument for its parameter. The extra arguments are ignored.
1. Hello, guest 2. Hello, Amrutha 3. Hello, Vineeth, Js ignores other arguments since only one parameter is passed, if we had used rest operator (...) with it then the other argument would have considered
Hello Guest: no any argument when we call greet function greet(), valueof argumentis equal to undefined. Hello Amrutha: value of argument is Amrutha. Hello Vineetha: here two argument but js take only name.
Hello, guest! Hello, Amrutha Hello, Vineeth JavaScript ignores the extra argument "How are you?".
As in JS it don’t enforce extra argument to function so it will simple consider first argument to be consoled here or rather to put …args to consider no of arguments