Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

What is const in JavaScript with example?

The const keyword was introduced in ES6 (2015). Variables defined with const cannot be Redeclared. Variables defined with const cannot be Reassigned. Variables defined with const have Block Scope.

What is const {} in JS?

Constants are block-scoped, much like variables declared using the let keyword. The value of a constant can't be changed through reassignment (i.e. by using the assignment operator), and it can't be redeclared (i.e. through a variable declaration).

What does const {} mean in node JS?

The curly braces around the variable name is called Destructuring assignment, and const {PI} = Math; will translate to const PI = Math.PI.


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);

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.


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


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

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