Last modified: May 13, 2026 By Alexander Williams
JavaScript Variable Declaration Guide
Variables are the building blocks of any JavaScript program. They store data values that you can reuse throughout your code. Understanding how to declare them correctly is essential for writing clean and bug-free code.
In JavaScript, you declare a variable using one of three keywords: var, let, or const. Each works differently regarding scope, reassignment, and hoisting. This guide explains all three in detail.
If you are new to coding, start with our JavaScript Variables Guide for a broader overview of variable types and naming rules.
Using var for Variable Declaration
The var keyword is the oldest way to declare a variable. It was used in JavaScript before ES6 (2015). While still functional, var has some quirks that can cause errors in larger projects.
A variable declared with var is function-scoped. This means it is available anywhere inside the function where it is defined, even before the line where it appears (hoisting).
Here is an example of declaring a variable with var:
// Declare a variable using var
var userName = "Alice";
console.log(userName); // Output: Alice
// var allows reassignment
var userName = "Bob"; // Redeclaration is allowed
console.log(userName); // Output: Bob
Notice that var lets you redeclare the same variable without error. This can lead to accidental overwrites in your code. For this reason, many developers avoid var in modern projects.
Using let for Block-Scoped Variables
The let keyword was introduced in ES6. It solves many problems of var. Variables declared with let are block-scoped, meaning they exist only inside the curly braces {} where they are defined.
let also does not allow redeclaration in the same scope. This makes your code more predictable and reduces bugs.
See the example below:
// Declare a variable using let
let age = 25;
console.log(age); // Output: 25
// let does not allow redeclaration in the same scope
// let age = 30; // This would throw an error: Identifier 'age' has already been declared
// Block scope example
if (true) {
let blockVar = "Inside block";
console.log(blockVar); // Output: Inside block
}
// console.log(blockVar); // Error: blockVar is not defined outside the block
Using let is the recommended way to declare variables that may change later. It keeps your code safe from unintended global exposure.
Using const for Constants
The const keyword stands for constant. Use it when you assign a value that should not be reassigned. Like let, const is block-scoped and does not allow redeclaration.
A variable declared with const must be initialized at the time of declaration. You cannot declare a const variable without giving it a value.
Here is how to use const:
// Declare a constant variable
const PI = 3.14159;
console.log(PI); // Output: 3.14159
// const cannot be reassigned
// PI = 3.14; // This throws an error: Assignment to constant variable.
// const must be initialized
// const myValue; // Error: Missing initializer in const declaration
// const with objects: properties can still be changed
const person = { name: "John", age: 30 };
person.age = 31; // Allowed
console.log(person.age); // Output: 31
Remember that const prevents reassignment of the variable binding, not the value itself. For objects and arrays, you can still modify their properties or elements.
Which Keyword Should You Use?
Modern JavaScript best practice is to use const by default. Only use let when you know the variable will be reassigned later. Avoid var in new code unless you are working on legacy projects.
This approach makes your intentions clear and reduces the chance of accidental bugs. For more details on variable behavior, read our JavaScript Variables Guide.
Variable Naming Rules
When declaring a variable, follow these simple rules:
- Names must begin with a letter, underscore (
_), or dollar sign ($). - Names cannot start with a number.
- Names are case-sensitive (
myVarandmyvarare different). - Use camelCase for multi-word names (e.g.,
firstName). - Avoid reserved keywords like
if,for, orclass.
Here is an example of valid variable names:
// Valid variable names
let firstName = "Jane";
let _count = 10;
let $price = 29.99;
let user2 = "Mike";
Common Mistakes to Avoid
Beginners often make these errors when declaring variables. Watch out for them:
- Forgetting to use a keyword (creates a global variable accidentally).
- Using
varinside loops (causes unexpected behavior due to hoisting). - Trying to reassign a
constvariable. - Declaring the same variable twice with
letorconst.
Always declare variables at the top of their scope for better readability. Our JavaScript Variables Guide covers more advanced tips on variable scope and hoisting.
Conclusion
Declaring a variable in JavaScript is straightforward once you understand the three keywords. Use const for values that stay the same, let for values that change, and avoid var in modern code. Always follow naming rules and be mindful of scope. With this knowledge, you can write cleaner, more reliable JavaScript from the start.