DEV Community

Richard
Richard

Posted on

4 3

JavaScript: Declaring Multiple Variables 📦📦📦

Let's say you want to declare some variables:

const name = "Orange County";
const state = "California"
let age = 132;
let population = 319000000;
Enter fullscreen mode Exit fullscreen mode

Four isn't that many, but if you had more, you'd need an easier way to do it. I think the best way is using destructuring. Note that with destructuring, you have to separate the let and const keywords.

const [name, state] = ["Orange County", "California"];
let [age, population] = [132, 319000000];
Enter fullscreen mode Exit fullscreen mode

You can also do this, also separating let and const keywords:

const name = "Orange County", state = "California;
let age = 132, population = 319000000;
Enter fullscreen mode Exit fullscreen mode

You can indent to make it easier to read. But let's be honest, this way is not really that different from declaring the variables individually. I don't personally see how it offers a significant advantage but it is an option.

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more