JavaScript
Function Parameters
Programming
Coding Best Practices
Web Development

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.

Browse interview questions

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.

javascript
1function greet(name = "stranger", punctuation = "!") {
2  return `Hello, ${name}${punctuation}`;
3}
4
5console.log(greet());
6console.log(greet("Ada"));
7console.log(greet("Ada", "?"));

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.

javascript
1function createUser({ name, admin = false, theme = "light", retries = 3 }) {
2  return { name, admin, theme, retries };
3}
4
5console.log(createUser({ name: "Lin" }));
6console.log(createUser({ name: "Lin", theme: "dark" }));

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:

javascript
function createUser({ name = "guest", admin = false } = {}) {
  return { name, admin };
}

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.

javascript
1function sum(...numbers) {
2  return numbers.reduce((total, value) => total + value, 0);
3}
4
5console.log(sum());
6console.log(sum(1, 2, 3, 4));

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:

javascript
1function connect(timeout) {
2  timeout = timeout || 5000;
3  return timeout;
4}

That seems fine until 0 is a valid value. Passing 0 still results in 5000 because 0 is falsy. The modern version is safer:

javascript
function connect(timeout = 5000) {
  return timeout;
}

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 || defaultValue when 0, 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 arguments object 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.