How to add optional first argument in function with JavaScript?

Sometimes, we want to add optional first argument in function with JavaScript.

In this article, we’ll look at how to add optional first argument in function with JavaScript.

How to add optional first argument in function with JavaScript?

To add optional first argument in function with JavaScript, we can make our function accept an object.

For instance, we write

const myFunction = (hash) => {
  const { options, content } = hash;
  //...
};

myFunction({ options, content });

to define the myFunction function that takes the hash object.

In it, we get the options and content properties from the hash.

And we call myFunction with an object with those 2 properties.

We can omit any property in the object we call myFunction with.

Conclusion

To add optional first argument in function with JavaScript, we can make our function accept an object.