Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone explain this JavaScript syntax? const { variable } = require( [duplicate]

I don't have much experience with javascript and I have been looking for an explanation into this syntax so if you know it would be much appreciated if you can help me understand it!

In Node.js

const { check, validationResult }   = require('express-validator/check');
const { matchedData, sanitize }     = require('express-validator/filter');

I am used to declaring variables like this:

const name = require('npm-module..');

Could someone explain this to me?

like image 891
Eduardo Carlos Avatar asked Nov 19 '25 12:11

Eduardo Carlos


1 Answers

It is called destructuring assignment.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

You are basically telling JS that require statement will return an object, and if it can pull out for you the property

check and validationResult in 2 const with the same name.

The syntax can go deeper with assigning default values or pulling out nested objects.

like image 97
AndreaBogazzi Avatar answered Nov 21 '25 02:11

AndreaBogazzi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!