const keyword usage in Javascript [duplicate] - javascript

This question already has answers here:
Javascript object bracket notation ({ Navigation } =) on left side of assign
(5 answers)
ES6 Object Destructuring Default Parameters
(1 answer)
Closed 6 years ago.
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?

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"

Related

I want to know the const declaration type of js [duplicate]

This question already has answers here:
Javascript object bracket notation ({ Navigation } =) on left side of assign
(5 answers)
What is destructuring assignment and its uses?
(3 answers)
Closed 3 years ago.
I know how to declare const variable like const a = 1;
but I dont get what is this
const { app, BrowserWindow } = require('electron')
can anyone explain me?
That’s a destructuring assignment.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
It lets you make shortcuts to members of an array or structure.

What does "const { PI } = Math" mean in JavaScript? [duplicate]

This question already has answers here:
Javascript object bracket notation ({ Navigation } =) on left side of assign
(5 answers)
Closed 4 years ago.
Are these constructions the same?
const {PI} = Math;
and
const PI = Math.PI;
What are benefits of using the first example?
The curly braces around the variable name is called Destructuring assignment,
and const {PI} = Math; will translate to const PI = Math.PI
It is a so-called "destructuring assignment".
Searching this site I found a similar question with a good answer:
Javascript (ES6) const with curly braces

What is this syntax in JavaScript [duplicate]

This question already has answers here:
Multiple assignment in JavaScript? What does `[ a, b, c ] = [ 1, 2, 3 ]` mean?
(4 answers)
Closed 5 years ago.
Take the following line of code
const [component] = router.getMatchedComponents({ ...to })
Could anyone advise what the square brackets around component means here? I have tried to google this but struggling to find an answer
It's called Destructuring assignment, and it's used to unpack the values of an array and assign them to new variables.
So here in your code:
const [component] = router.getMatchedComponents({ ...to })
You are assigning to the component variable the first element held in the array that will be returned by router.getMatchedComponents({...to}), where to is an array-like structure turned into object using the spread operation.

Is this a new way to extend an object in JavaScript? [duplicate]

This question already has answers here:
Javascript object bracket notation ({ Navigation } =) on left side of assign
(5 answers)
Closed 5 years ago.
While upgrading a private yeoman generator I stumpled upon this statement while looking through the official generator-webapp:
const { features } = answers;
I wasn't able to find anything about this, aside from the fact that it only works on node >=6.
What does this statement do? Where is this defined?
It's destructuring assignment. It's equivalent to:
const features = answers.features;
It was introduced in ES2015.
This:
const { features } = answers;
Is the shorthand of this:
const features = answers.features;
You could also declare many variables in a single line, see following please:
var answers = {"features": "test"};
const { features } = answers;
console.log(features);
var longObj = {"attr1": "val1", "attr2" : "val2"};
const { attr1, attr2 } = longObj;
console.log(attr1, attr2);
I hope it was clear. Bye.

Variable declaration in reactjs documentation [duplicate]

This question already has answers here:
Javascript object bracket notation ({ Navigation } =) on left side of assign
(5 answers)
Closed 7 years ago.
react.js documentation has following variable declarations:
var { Image, StyleSheet, Text, View } = React;
Could you tell what does it meant? Thank you.
This is the destructuring syntax, which is part of ES6.
It's a feature of ES6 called destructuring. Essentially the same as writing:
var Image = React.Image;
var StyleSheet = React.StyleSheet;
var Text = React.Text;
var View = React.View;

Categories

Resources