1

I came across the following code:

const { userinfo } = userInfo;

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

ha3an
  • 72
  • 2
  • 15
  • Then there is also this one: https://stackoverflow.com/questions/33798717/javascript-es6-const-with-curly-braces – JΛYDΞV 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
  • 42,814
  • 35
  • 154
  • 218