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)

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay