Declaring Multiple Variables in a Single Statement in JavaScript

Consider the following code snippet:

let count = length, temp, index;

In this code, the variable count receives 3 values separated by commas. What is the final value of count?

Variable declarations (var, let, and const) support declaring multiple variables in a single statement. Each additional variable beyond the first is separated by a comma. These variables may or may not be defined with an initial value using the assignment operator (=), except in the case of const, where it's required.

The statement

let count = length, temp, index;

is effectively equivalent to

let count = length;
let temp;
let index;

So, the value of count will be length.

Get my free, weekly JavaScript tutorials

Want to improve your JavaScript fluency?

Every week, I send a new full-length JavaScript article to thousands of developers. Learn about asynchronous programming, closures, and best practices ā€” as well as general tips for software engineers.

Join today, and level up your JavaScript every Sunday!

Thank you, Taha, for your amazing newsletter. Iā€™m really benefiting from the valuable insights and tips you share.

- Remi Egwuda