DEV Community

Richard
Richard

Posted on

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)