0

I am going through an online article and I came across

const mongoose = require('mongoose');

const { Schema } = mongoose;

May I know what is the meaning of const { Schema } = mongoose;.

Are we doing object initiation of mongoose to Schema variable or ?

Raja G
  • 5,973
  • 14
  • 49
  • 82
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Object_destructuring – Teemu May 13 '20 at 14:25
  • It's called *decomposition*. That `const` declaration means to create a variable called `Schema` whose value is that of the property with the same name in the object on the right-hand side of `=`. – Pointy May 13 '20 at 14:26
  • 1
    Does this answer your question? [what is the difference between const and const {} in javascript](https://stackoverflow.com/questions/41058569/what-is-the-difference-between-const-and-const-in-javascript) – 0stone0 May 13 '20 at 14:26
  • Or https://stackoverflow.com/q/54605286/691711 – zero298 May 13 '20 at 14:27

1 Answers1

4

In JavaScript, the const { Schema } = mongoose; syntax is something called object destructuring.

You can read more about it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

But this statement is exactly equivalent to this:

const Schema = mongoose.Schema;
Jake
  • 111
  • 3