Arjan van der Laan
Arjan van der Laan

Reputation: 21

What do curly braces do in a function call (as argument, NOT a parameter) in Javascript

So I have to work with a function call like this:

someFunction({ someVar });

My question is what do the curly braces do, why use them?

Upvotes: 0

Views: 616

Answers (2)

MSS
MSS

Reputation: 11

When you include curly braces in a function call as argument it passes an object as the argument and the key is same as the name of the variable and value as the value stored in the variable. This is a shorthand for Object initialization. See below snippet for the reference.

function func(argument) {
  console.log(argument)
}

const variable = "value"
func({variable})

you should use this type of argument if you want to pass some value with some unique identifier as key in the function. this type of argument is widely used in reactjs for passing props in the functional components.

Upvotes: 0

ruohola
ruohola

Reputation: 24018

It passes an object as the argument. The object has one property named 'someVar' which has the value of the someVar variable.

const someVar = 'foo';
console.log({ someVar });

Upvotes: 4

Related Questions