1

I came across the following code:

const { userinfo } = userInfo;

I'm wondering, why do we use curly brackets with the const here?

ha3an
  • 70
  • 1
  • 14
  • Then there is also this one: https://stackoverflow.com/questions/33798717/javascript-es6-const-with-curly-braces – j D3V Apr 18 '22 at 07:49

1 Answers1

3

In Javascript this syntax is called destructuring assignment and more specifically object destructing. Let's say you have an object:

const obj = { data: {...}, user: {...} }

For instance, when you write:

const { data, user } = obj
console.log(data)

Here you define new variables data and user and assign corresponding property values from the object obj to those variables.


It seems like in your case you have a local variable:

userInfo = {userInfo: {...} }
const { userinfo } = userInfo;

Deconstructing userInfo into { userInfo } would give you an error:

Uncaught SyntaxError: Identifier 'userInfo' has already been declared

Andrei
  • 40,274
  • 34
  • 151
  • 206