Is there a better way to do optional function parameters in JavaScript?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Yes, there are better ways than manually checking every missing argument inside the function body. Modern JavaScript gives you default parameters, rest parameters, and object destructuring, and choosing the right one depends on whether you want simple defaults, a variable-length list, or named options.
Use Default Parameters for Simple Cases
If the function has a few positional arguments and each one has a sensible fallback, default parameters are the cleanest answer.
This is much clearer than old patterns such as name = name || "stranger", which break when valid falsy values such as 0, false, or an empty string are intentional.
Use an Options Object for Many Optional Values
Once a function has several optional settings, positional arguments become hard to read. An options object is usually the better design because callers can name what they are passing.
This scales much better than a signature like createUser(name, admin, theme, retries) because order stops being the main source of meaning.
You can also give the whole object a default so the function survives being called with no argument at all:
That small = {} detail prevents destructuring from failing when the caller omits the options object.
Use Rest Parameters for Variable-Length Input
Optional parameters are not the same as "any number of values." If the function naturally accepts a flexible list, use rest parameters instead.
This is cleaner than the old arguments object because rest parameters create a real array and make the function signature self-documenting.
Avoid the Old || Default Pattern
Before default parameters existed, code often looked like this:
That seems fine until 0 is a valid value. Passing 0 still results in 5000 because 0 is falsy. The modern version is safer:
Now only undefined triggers the default.
Which Pattern Should You Choose?
A simple rule usually works:
- use default parameters for a small number of positional arguments
- use an options object for many optional settings
- use rest parameters for a variable-length list
That keeps the function signature aligned with the shape of the problem instead of forcing one pattern everywhere.
Common Pitfalls
- Using positional parameters for too many optional settings, which makes calls hard to read.
- Using
value = value || defaultValuewhen0,false, or""are valid inputs. - Forgetting to default an options object to
{}before destructuring it. - Using rest parameters when the function really wants named settings, not a list.
- Keeping the old
argumentsobject pattern in new code without a specific reason.
Summary
- Default parameters are the best choice for simple optional positional arguments.
- An options object is better when a function has many optional settings.
- Rest parameters are for variable-length lists, not named options.
- Modern defaults are safer than the old
||fallback pattern. - Pick the parameter style that matches the shape of the API you want callers to use.
Related reading
- Is there a built-in function to print all the current properties and values of an object?
- Is there a CSS selector for elements containing certain text?
- Is there a good object mapper for Amazons dynamodbthrough aws sdk which can be used in nodejs?
- Is there a good way to Promise.all an array of objects which has a property as promise?
- Is there a JavaScript preprocessor that makes callbacks look nice?
- Is there a "null coalescing" operator in JavaScript?
- Is there a shortcut to create padded array in JavaScript?
- Is there a Thrift or Cassandra client for Node.js/JavaScript
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.