What is the difference between const and const {} in JavaScript What is the difference between const and const {} in JavaScript node.js node.js

What is the difference between const and const {} in 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 destructuringconst { name } = obj;console.log("name", name);//assigning multiple variables at one timeconst { age, id } = obj;console.log("age", age);console.log("id", id);//using different names for the propertiesconst { name: personName } = obj;console.log("personName", personName);