Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript (ES6) const with curly braces [duplicate]

People also ask

What does Const with curly braces mean in JavaScript?

It's a destructuring assignment. Specifically, an object destructuring assignment. It might help to see it rewritten in a more verbose way.

When should I use curly braces for ES6 import?

The curly braces are used only for import when export is named. If the export is default then curly braces are not used for import.

Is const a ES6 feature?

Introduction to the JavaScript const keywordES6 provides a new way of declaring a constant by using the const keyword. The const keyword creates a read-only reference to a value. By convention, the constant identifiers are in uppercase. Like the let keyword, the const keyword declares blocked-scope variables.

Does JavaScript need curly braces?

Yes, it works, but only up to a single line just after an 'if' or 'else' statement. If multiple lines are required to be used then curly braces are necessary. if(foo) Dance with me; else Sing with me; The following will NOT work the way you want it to work.


It's a destructuring assignment. Specifically, an object destructuring assignment.

It might help to see it rewritten in a more verbose way.

const abc = Object.abc;
const def = Object.def;

It's a shorthand way to initialise variables from object properties.

const name = app.name;
const version = app.version;
const type = app.type;

// Can be rewritten as:
const { name, version, type } = app;

You can do the same kind of thing with arrays, too.

const a = items[0];
const b = items[1];
const c = items[2];

// Can be written as:
const [a, b, c] = items;