Categories
JavaScript Answers

How to add optional first argument in function with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *