1

I have been seeing the use of const as shown in the the code below in ReactJS. So far I know it is about destructuring from this https://github.com/lukehoban/es6features#destructuring and this

Scenario 1

class DoSomething extends Component() {
  constructor() {
    this.state = {
      jump: ''
    }
  }

  function somethingSmall() {
    // I can access the state with the declaration that uses the curly braces
    const { jump } = this.state;
  }
}

In the example below, I noticed it when using NodeJS and it also uses const. I was trying to access metadata upon uploading a file to Google Cloud Storage.

Scenario 2

bucket = storage.bucket(bucket_name);
bucket.upload(filepath)
.then(() => {
  var uploaded_file = bucket.file(filename);
  const [ metadata ] = await uploaded_file.getMetadata()
 }
)

Please clarify the use of the curly and square brackets. Any help will appreciated including any links for further reading.

K_Wainaina
  • 69
  • 5
  • 2
    `{ }` used when you are destructuring from an object and `[ ]` when you are destructuring from an array – Noob Dec 03 '19 at 16:19

1 Answers1

1

{ } should be used when you are destructuring from an object and [ ] when you are destructuring from an array.

ruby_newbie
  • 2,958
  • 2
  • 15
  • 25