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

Time:08-13

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?

CodePudding user response:

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 });

CodePudding user response:

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. 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.

please upvote if you find the answer useful.

  • Related