undefined in Function Parameters and Returns in JavaScript
undefined appears in parameters and return values in specific ways. Here is what to expect and how to handle it.
undefined in Function Parameters and Returns in JavaScript
undefined shows up in function parameters and return values in specific, predictable ways. Knowing these prevents bugs.
Missing Parameters Become undefined
If you call a function with fewer arguments than parameters, the missing ones are undefined:
function greet(name, greeting) { console.log(greeting, name); } greet("Kunal"); // undefined Kunal (greeting is undefined) `` ### Default Parameters Replace `undefined` ```js function greet(name, greeting = "Hi") { console.log(greeting, name); } greet("Kunal"); // Hi Kunal greet("Kunal", undefined); // Hi Kunal (default applies) greet("Kunal", null); // null Kunal (default does not apply) greet("Kunal", ""); // (empty) Kunal (default does not apply) `` Defaults apply only when the argument is `undefined` or omitted. ### `arguments` Object and Missing Parameters In regular functions, `arguments.length` tells you how many were actually passed: ```js function foo(a, b) { console.log(arguments.length); // 1 (only a was passed) console.log(b); // undefined } foo(1); `` ### Functions Without `return` Return `undefined` ```js function logHi() { console.log("hi"); } const result = logHi(); // logs "hi", result is undefined `` ### Functions With Empty `return` ```js function earlyExit(condition) { if (!condition) return; // returns undefined return "done"; } `` ### Arrow Functions With Block Bodies ```js const foo = () => { const x = 5; // no return statement }; foo(); // undefined const bar = () => { return 5; }; bar(); // 5 `` Arrow shorthand returns implicitly: ```js const baz = () => 5; baz(); // 5 `` ### Detecting Missing Arguments ```js function process(data, options) { if (options === undefined) { options = { default: true }; } // or use defaults: function process(data, options = { default: true }) } `` ### The Takeaway Missing parameters are `undefined`. Default parameters apply only for `undefined` (not `null` or `0`). Functions without `return` return `undefined`. `arguments.length` tells you how many arguments were actually passed. Use defaults or explicit checks to handle missing inputs.
The missing parameters are set to undefined. You can handle this with default parameters, which apply only when the argument is undefined or omitted.
No. Defaults apply only when the argument is undefined or omitted. Passing null, 0, or empty string does not trigger the default, because those are real values.
undefined. If a function has no return, or a return with no expression, it returns undefined to the caller.
Check if the parameter === undefined, or use a default parameter. You can also check arguments.length (in regular functions) to see how many arguments were actually passed.
Yes. An arrow with a block body (=> { ... }) needs an explicit return. Without it, the function returns undefined. Only an arrow with an expression body (=> expr) returns the expression implicitly.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

