How to Declare a Variable in JavaScript
What you’ll build or solve
You’ll declare variables that store values correctly and follow modern JavaScript best practices.
When this approach works best
Use this approach when you:
Learn JavaScript on Mimo
- Store user input, configuration values, or calculation results.
- Track state inside loops or conditionals.
- Define constants like API URLs or tax rates.
Prefer modern syntax in new projects. Avoid var unless maintaining older codebases.
Prerequisites
- A
.jsfile or an HTML file with a<script>tag - Basic knowledge of strings, numbers, and arrays
No additional setup is required.
Step-by-step instructions
1) Choose the right declaration keyword
JavaScript has three keywords for declaring variables. Pick the one that matches your intent.
Option A: Use const by default
Use const for values that should not be reassigned.
consttaxRate=0.2;
constname="Alex";
Reassignment causes an error:
consttaxRate=0.2;
// taxRate = 0.3; // Error
Use const unless you know the value must change.
Option B: Use let when reassignment is required
Use let if the variable’s value will change later.
letscore=10;
score=20;
You can also declare first and assign later:
lettotal;
total=100;
Switch to let only when reassignment is needed.
Option C: Use var only for legacy code
var works differently from let and const.
varcount=5;
Avoid var in modern development because it does not respect block scope.
What to look for
Declare and assign in one line:
constuser="Sam";
Or declare first, assign later:
letresult;
result=42;
let and const are block-scoped:
if (true) {
constmessage="Hello";
console.log(message);
}
// console.log(message); // Error
var ignores block scope:
if (true) {
vartest="Visible outside";
}
console.log(test);
You cannot redeclare let or const in the same scope:
letprice=100;
// let price = 200; // Error
const prevents reassignment, not mutation:
constuser= { name:"Sam" };
user.name="Alex";// Allowed
Declare variables before using them to avoid temporal dead zone errors:
lettotal=50;
console.log(total);
Examples you can copy
Example 1: Counter that changes
letcounter=0;
counter+=1;
counter+=1;
console.log(counter);
Use let because the value updates.
Example 2: Configuration value
constAPI_URL="https://api.example.com";
console.log("Fetching from:",API_URL);
Use const for values that should stay fixed.
Example 3: Loop variable
for (leti=0;i<3;i++) {
console.log(i);
}
// console.log(i); // Error
If you use var instead:
for (vari=0;i<3;i++) {
console.log(i);
}
console.log(i);// Accessible here
This difference affects how your code behaves.
Common mistakes and how to fix them
Mistake 1: Reassigning a const variable
What you might do:
constrate=0.2;
rate=0.3;
Why it breaks: const variables cannot be reassigned.
Fix:
letrate=0.2;
rate=0.3;
Mistake 2: Using var inside blocks
What you might do:
if (true) {
varmessage="Hi";
}
Why it breaks: var ignores block scope and may leak outside the block.
Fix:
if (true) {
letmessage="Hi";
}
Mistake 3: Accessing before declaration
What you might do:
console.log(total);
lettotal=50;
Why it breaks: let and const cannot be used before declaration.
Fix:
lettotal=50;
console.log(total);
Troubleshooting
If you see “Assignment to constant variable,” change const to let.
If you see “Cannot access before initialization,” move the declaration above usage.
If a variable is accessible outside a block unexpectedly, check for var.
If changes do not appear in the browser, refresh the page.
If Node shows errors, confirm you are running the correct file.
Quick recap
- Use
constby default. - Use
letwhen reassignment is needed. - Avoid
varin modern code. - Remember block scope rules.
- Declare variables before using them.
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot