What is the difference between const and const {} in JavaScript

Javascriptnode.jsElectronDestructuring

Javascript Problem Overview


When I study electron, I found 2 ways of getting BrowserWindow object.

const {BrowserWindow} = require('electron')

and

const electron = require('electron')
const BrowserWindow = electron.BrowserWindow

What is the difference between const and const {} in JavaScript?

I can't understand why the const {} can work. Do I miss anything important about JS?

Javascript Solutions


Solution 1 - Javascript

The two pieces of code are equivalent but the first one is using the ES6 destructuring assignment to be shorter.

Here is a quick example of how it works:

const obj = {
  name: "Fred",
  age: 42,
  id: 1
}

//simple destructuring
const { name } = obj;
console.log("name", name);

//assigning multiple variables at one time
const { age, id } = obj;
console.log("age", age);
console.log("id", id);

//using different names for the properties
const { name: personName } = obj;
console.log("personName", personName);

Solution 2 - Javascript

const {BrowserWindow} = require('electron')

Above syntax uses ES6. If you have an object defined as:

const obj = {
    email: "[email protected]",
    title: "Hello world"
}

Now if we want to assign or use email and title field of obj then we don't have to write the whole syntax like

const email = obj.email;
const title = obj.title;

This is old school now.

We can use ES6 Destructuring assignment i.e., if our object contains 20 fields in obj object then we just have to write names of those fields which we want to use like this:

const { email,title } = obj;

This is ES6 syntax-simpler one It will automatically assign email and title from obj, just name has to be correctly stated for required field.

Solution 3 - Javascript

This is one of the new features in ES6. The curly braces notation is a part of the so called destructuring assignment. What this means is that, you no longer have to get the object itself and assign variables for each property you want on separate lines. You can do something like:

const obj = {
  prop1: 1,
  prop2: 2
}

// previously you would need to do something like this:
const firstProp = obj.prop1;
const secondProp = obj.prop2;
console.log(firstProp, secondProp);
// etc.

// however now you can do this on the same line:
const {prop1, prop2} = obj;
console.log(prop1, prop2);

As you have seen in the end the functionality is the same - simply getting a property from an object.

There is also more to destructuring assignment - you can check the entire syntax in MDN: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

Solution 4 - Javascript

Other answers are good enough. I would suggest some useful features of Destructuring assignment

Firstly, Let's look at the following define:

> The destructuring assignment syntax is a JavaScript expression that > makes it possible to unpack values from arrays, or properties from > objects, into distinct variables.

Features:

  1. Destructure an array, index of each item in array act as property (Due to an Array is an object in JavaScript)
> const {0: first, 1: second} = [10, 20]
console.log(first);   // 10
console.log(second);  // 20

  1. Combine with Spread ... operator
> {a, b, ...rest} = {a: 10, b: 20, c: 30, d: 40}
console.log(a); // 10
console.log(b); // 20
console.log(rest ); // {c: 30, d: 40}
  1. Default values
const {a = 10, b = 20} = {a: 1};

console.log(a); // 1
console.log(b); // 20
  1. Assigning to new variable names
const {p: a, q: b} = {p: 10, q: 20};

console.log(a); // 10
console.log(b); // 20

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKevin00000000View Question on Stackoverflow
Solution 1 - JavascriptVLAZView Answer on Stackoverflow
Solution 2 - JavascriptGaurav SachdevaView Answer on Stackoverflow
Solution 3 - JavascriptVasil DininskiView Answer on Stackoverflow
Solution 4 - JavascriptNguyễn Văn PhongView Answer on Stackoverflow