1

I have used variable declaration in JavaScript only as - const name = require("something");

  1. But what does curly braces means in const { name } = require('something'). ?
  2. And why some declaration have parentheses () in its end like- const name = require('something')(); ?

What are the significance of both and where to use them while declaring variables? Thanks in advance!

ADITYA HAZARIKA
  • 300
  • 1
  • 4
  • 13
  • Does this answer your question? [Object destructuring syntax - ES6](https://stackoverflow.com/questions/31915621/object-destructuring-syntax-es6) – Dominik Matis Sep 07 '20 at 08:04
  • 1
    The first is called [destructuring](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment), the second is requiring a function/class and calling/instanciating it right away. –  Sep 07 '20 at 08:04

1 Answers1

2

const { name } = require('something') is destructuring a const called name from whatever object is returned from the require statement. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

Adding the parenthesis is going to execute whatever function is returned from the require statement

andy mccullough
  • 9,070
  • 6
  • 32
  • 55