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.