Default Parameters — JS Technical Interview Questions & Answers
Fallback values when an argument is omitted.
Default Parameters
- Default Parameters (js) is a JS topic. In plain words you use it for a short Default Parameters js example in a script in the browser. Don’t start with a slogan — start with that picture.
- Smallest example: the smallest Default Parameters js example you can type. Type it, run it, and say what you see. If you can do that from memory, you know Default Parameters (js).
- From the example next to this theory: Fallback values when an argument is omitted.
- Trap: only saying “Default Parameters js” with no example. Fix that before you talk about advanced JS.
- Viva: what is Default Parameters (js)? Then show the smallest Default Parameters js example you can type. Then name the trap.
Technical Interview Tip: What is Default Parameters (js)? Show this: the smallest Default Parameters js example you can type. Trap: only saying “Default Parameters js” with no example.
Code Examples for Default Parameters
Default Parameters — working example
function greet(name = "friend") {
return "Hi " + name;
}
const shout = function (msg) {
return msg.toUpperCase();
};
(function () {
console.log("IIFE ran");
})();
function mapNums(nums, fn) {
const out = [];
for (const n of nums) out.push(fn(n));
return out;
}
console.log(greet("Ada"), shout("js"));
console.log(mapNums([1, 2], (n) => n * 2));
function factorial(n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
console.log(factorial(4));Default Parameters — functions take parameters, can be expressions/IIFEs/callbacks, and recurse with a base case. Pure functions avoid side effects.
How to Explain Default Parameters to an Interviewer
What is Default Parameters (js)?
Used for a short Default Parameters js example a script in the browser Not just the heading
Give one small example of Default Parameters (js).
the smallest Default Parameters js example you can type Fallback values when an argument is omitted
What mistake do beginners make with Default Parameters (js)?
only saying “Default Parameters js” with no example Show the fix
Where do you use Default Parameters (js)?
a script in the browser a short Default Parameters js example