Last modified: May 26, 2026
JavaScript Variables Examples
Variables are the building blocks of JavaScript. They store data values. This guide shows you real examples. You will learn how to use var, let, and const. Each example is simple and easy to follow.
What is a Variable?
A variable is a container for data. You give it a name. Then you put a value inside. JavaScript uses the = sign to assign values. For example:
// Declare a variable named 'name'
let name = "Alice";
console.log(name); // Output: Alice
Alice
Here, name holds the string "Alice". You can use it anywhere in your code. Variables make code reusable and easy to read.
Declaring Variables with var
The var keyword is the old way. It has function scope. This means it works inside the function where it is declared. Example:
var age = 25;
console.log(age); // Output: 25
25
But var can cause bugs. It hoists to the top of its scope. This can lead to unexpected results. Modern JavaScript prefers let and const. For more details, check our JavaScript Variable Declaration Guide.
Using let for Block Scope
The let keyword is modern. It has block scope. This means it only works inside the curly braces {} where it is defined. Example:
let score = 100;
if (true) {
let score = 200; // This is a different variable
console.log(score); // Output: 200
}
console.log(score); // Output: 100
200
100
Notice the inner score does not affect the outer one. This prevents errors. Use let when the value changes later.
Constants with const
The const keyword is for values that never change. You must assign a value when you declare it. Example:
const pi = 3.14159;
console.log(pi); // Output: 3.14159
// pi = 3.14; // This will throw an error
3.14159
But const does not make objects immutable. You can still change properties of an object. Example:
const person = { name: "Bob", age: 30 };
person.age = 31; // This is allowed
console.log(person.age); // Output: 31
31
Use const by default. Only use let when you need to reassign. This makes your code safer.
Variable Types in JavaScript
JavaScript is dynamically typed. A variable can hold any type. The type changes when you assign a new value. Common types are strings, numbers, booleans, arrays, and objects. Example:
let data = "Hello"; // string
data = 42; // now number
data = true; // now boolean
console.log(data); // Output: true
true
This flexibility is powerful. But be careful. It can cause bugs if you expect one type and get another. For a deeper dive, see our JavaScript Variable Types Guide.
Variable Scope Explained
Scope determines where a variable is accessible. There are three types: global, function, and block. Global variables are available everywhere. Function variables are only inside the function. Block variables are only inside the block. Example:
let globalVar = "I am global";
function testScope() {
let functionVar = "I am local";
if (true) {
let blockVar = "I am block scoped";
console.log(globalVar); // Works
console.log(functionVar); // Works
console.log(blockVar); // Works
}
console.log(globalVar); // Works
console.log(functionVar); // Works
// console.log(blockVar); // Error: blockVar is not defined
}
testScope();
I am global
I am local
I am block scoped
I am global
I am local
Understanding scope prevents many errors. For a complete overview, read our JavaScript Variable Scope Explained.
Variables as Strings
Strings are text data. You use quotes around them. Single or double quotes work. Example:
let greeting = "Hello, world!";
let name = 'John';
console.log(greeting + " " + name); // Output: Hello, world! John
Hello, world! John
You can also use template literals with backticks. They allow variables inside strings. Example:
let age = 25;
let message = `I am ${age} years old.`;
console.log(message); // Output: I am 25 years old.
I am 25 years old.
Template literals make string building easier. For more examples, see our JavaScript Variable as String Guide.
Common Mistakes with Variables
Beginners often make these errors. First, forgetting to declare a variable. This creates a global variable by accident. Always use let or const. Second, misspelling variable names. JavaScript is case-sensitive. myVar and myvar are different. Third, using var in loops. This can cause unexpected behavior. Example of a common bug:
// Bad: using var in a loop
for (var i = 0; i < 3; i++) {
setTimeout(function() {
console.log(i); // Output: 3, 3, 3
}, 100);
}
3
3
3
Fix it by using let:
// Good: using let in a loop
for (let i = 0; i < 3; i++) {
setTimeout(function() {
console.log(i); // Output: 0, 1, 2
}, 100);
}
0
1
2
Always choose let for loops. This ensures each iteration has its own copy of the variable.
Best Practices for Variables
Follow these rules for clean code. Use meaningful names. userAge is better than ua. Use camelCase for variable names. Start with a lowercase letter. Avoid reserved words like class or return. Declare variables at the top of their scope. This makes code easier to read. Use const as the default. Only use let when you need to reassign. Never use var in new projects.
Conclusion
JavaScript variables are simple but powerful. You learned var, let, and const with examples. You saw how scope works. You practiced with strings and types. Use const by default and let when needed. Avoid var. This makes your code safer and easier to debug. Keep practicing with the examples above. You will master variables quickly.