Categories
JavaScript Answers

How to pass an object to a JavaScript function?

Spread the love

Sometimes, we want to pass an object to a JavaScript function.

In this article, we’ll look at how to pass an object to a JavaScript function.

How to pass an object to a JavaScript function?

To pass an object to a JavaScript function, we can add a parameter that accepts an object.

For instance, we write

const someFunc = (arg) => {
  alert(arg.foo);
  alert(arg.bar);
};

someFunc({ foo: "This", bar: "works!" });

to define the someFunc function that takes the arg object argument.

Then we get the properties with arg.foo and arg.bar.

Next we call someFunc with an object with the properties.

Conclusion

To pass an object to a JavaScript function, we can add a parameter that accepts 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 *