Last modified: Jul 17, 2026
JS Multiple Variable Declaration Guide
Declaring variables is a core part of JavaScript. You often need to create several variables at once. Doing it the right way keeps your code clean and readable.
This guide covers everything about declaring multiple variables in JavaScript. You will learn different syntax styles, best practices, and common mistakes to avoid.
By the end, you will write more efficient and organized JavaScript code. Let's dive in.
Why Declare Multiple Variables?
Declaring multiple variables in one statement saves time and reduces code repetition. It also groups related variables together.
For example, imagine setting up configuration values for a game. You might need score, lives, and level. Declaring them together makes sense.
// Declaring three related variables in one line
let score = 0, lives = 3, level = 1;
console.log(score); // 0
console.log(lives); // 3
console.log(level); // 1
This approach is especially useful when initializing multiple variables at the start of a function or script.
The Basic Syntax
The most common way is to use a single var, let, or const keyword, followed by a comma-separated list of variables.
Each variable can be assigned a value immediately. You can also declare a variable without a value, leaving it as undefined.
// Using let for multiple declarations
let firstName = "John", lastName = "Doe", age = 30;
// Declaring without initial values
let a, b, c;
console.log(firstName); // John
console.log(a); // undefined
Important: Always use let or const instead of var in modern JavaScript. var has function scope, which can cause unexpected bugs.
Using const for Multiple Variables
The const keyword declares constants that cannot be reassigned. You can declare multiple const variables in one statement, but each must be initialized immediately.
// Declaring multiple constants
const PI = 3.14159, E = 2.71828, GOLDEN_RATIO = 1.618;
console.log(PI); // 3.14159
console.log(E); // 2.71828
Remember, const only prevents reassignment. If the value is an object or array, its properties can still be modified.
Best Practices for Readability
While declaring multiple variables in one line is concise, it can hurt readability if the line becomes too long. A common best practice is to use separate lines for each variable, but still use a single keyword.
This style improves clarity and makes it easier to add comments for each variable.
// Clean and readable style
let firstName = "John",
lastName = "Doe",
age = 30;
// Each variable on its own line
const API_URL = "https://api.example.com",
TIMEOUT = 5000,
RETRIES = 3;
Tip: Some developers prefer to always declare variables individually. Choose the style that works best for your team and project.
Common Mistakes to Avoid
One common mistake is mixing let and const in the same statement. This is not allowed. Each declaration statement must use only one keyword.
// This will cause a syntax error
// let a = 1, const b = 2; // SyntaxError
// Correct way: separate statements
let a = 1;
const b = 2;
Another mistake is forgetting to initialize a const variable. This also throws an error.
// This will cause a syntax error
// const x, y; // SyntaxError: Missing initializer in const declaration
// Correct way: always initialize const
const x = 10, y = 20;
Also, be careful with variable scope. Variables declared with let and const are block-scoped. This means they only exist inside the block (e.g., inside a loop or if statement) where they are defined.
For a deeper dive into how variable scope works, check out our JavaScript Variable Scope Explained guide.
Destructuring for Multiple Variables
JavaScript also offers destructuring assignment. This is a powerful way to extract values from arrays or objects into multiple variables at once.
It is not exactly the same as declaring multiple variables with commas, but it serves a similar purpose.
// Array destructuring
let [x, y, z] = [10, 20, 30];
console.log(x); // 10
console.log(y); // 20
console.log(z); // 30
// Object destructuring
let person = { name: "Alice", age: 25 };
let { name, age } = person;
console.log(name); // Alice
console.log(age); // 25
Destructuring is especially useful when working with function returns or API responses.
Multiple Declarations in Loops
You can also declare multiple variables in the initialization part of a for loop. This is common when you need a counter and a limit variable.
// Declaring multiple variables in a for loop
for (let i = 0, len = 5; i < len; i++) {
console.log(i);
}
// Output: 0, 1, 2, 3, 4
This keeps the loop logic self-contained and easy to read.
Variable Naming Rules
When declaring multiple variables, follow the same naming rules as for single variables. Names must start with a letter, underscore, or dollar sign. They cannot contain spaces or hyphens.
For a complete list of naming conventions, read our JavaScript Variable Naming Rules guide.
// Good variable names
let userName = "Bob", _count = 10, $price = 99.99;
// Bad variable names (will cause errors)
// let 2fast = "speed"; // SyntaxError
// let my-name = "test"; // SyntaxError
Using clear and descriptive names makes your code self-documenting.
Variable Shadowing
Be aware of variable shadowing when declaring multiple variables. This happens when a variable in an inner scope has the same name as a variable in an outer scope.
This can lead to confusing bugs. Learn more in our JavaScript Variable Shadowing article.
let name = "Global";
function test() {
let name = "Local"; // This shadows the global variable
console.log(name); // Local
}
test();
console.log(name); // Global
Always be mindful of scope to avoid unintended shadowing.
Performance Considerations
Declaring multiple variables in one statement does not offer significant performance benefits over separate statements. Modern JavaScript engines optimize both approaches equally.
Focus on readability and maintainability instead of micro-optimizations.
Conclusion
Declaring multiple JavaScript variables is a simple yet powerful technique. You can use a single let, const, or var keyword with comma-separated variables.
Remember these key points:
- Use
letandconstinstead ofvar. - Always initialize
constvariables. - Keep your code readable by placing each variable on a new line if needed.
- Avoid mixing keywords in the same statement.
- Be mindful of scope and shadowing.
Mastering variable declaration is a fundamental step in becoming a proficient JavaScript developer. Practice these patterns in your projects, and your code will become cleaner and more efficient.
For more examples and practice, check out our JavaScript Variables Examples page.