Y
Y
Yuriy602017-09-20 23:18:31
JavaScript
Yuriy60, 2017-09-20 23:18:31

How does declaring variables like "const { x, y } = function()" work?

I found here such declaration of variables

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

How it works and how it differs from the usualvar x = something

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stalker_RED, 2017-09-20
@Yuriy60

It differs in that more than one variable is returned, an object with named fields that expand into variables. It works something like this:

const {a, b} = {a:'foo', b:'bar'}
console.log(a) // -> foo
console.log(b) // -> bar

This is called destructuring .

S
shelomanovd, 2017-09-21
@shelomanovd

This is destructuring. This is the same as

var obj = {a: 10, b: 15};
var a = obj.a;
var b = obj.b

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question