Daniel Bastos Daniel Bastos - 2 months ago 6
Javascript Question

const keyword usage in Javascript

I was reading the source for a MeteorJS package (this one) when I came across this:

const {
getMeteorData,
pure = true,
} = expandedOptions;


I did some research to figure out what this code would do, but couldn't find any other instance of const being used like this, or any documentation to clarify it for me.

Any hints, please?

Answer

It is using es6 to unpack (or destructure) the values inside the expandedOptions object.

const val = {a:1, b:"hello"};
const {
    a,
    b
} = val;

console.log(a); //1
console.log(b); //"hello"
Comments