Containers for Storing Data
JavaScript Variables
Variables are named containers for values. Learn how to declare them with `let` and `const`, and why `var` belongs in the history books.
What you'll learn
- Declare variables with `let` and `const`
- Know when to use `const` vs `let`
- Understand block scope
- Recognize why `var` should be avoided in new code
A variable is a name that refers to a value. Once you put a value in a variable, you can use the name anywhere in your code.
let greeting = "Hello, world!";
console.log(greeting); We declared a variable called greeting, assigned it the string
"Hello, world!", and then used it with console.log. Three steps,
one line.
Three Ways to Declare
JavaScript has three keywords for declaring a variable: let, const,
and var. They look almost identical β but the behavior is different
in important ways.
let count = 0; // a value that will change
const PI = 3.14159; // a value that will never change
var legacy = "old style"; // the original (1995) way
count = count + 1;
console.log(count, PI, legacy); const vs let
const declares a value that cannot be reassigned. let declares
one that can. Thatβs the only difference.
const PI = 3.14159;
// PI = 3; // β TypeError: Assignment to constant variable.
let count = 0;
count = 1; // β
fine β let allows reassignment
console.log(count); A subtle point about const objects
const makes the binding constant, not the value. If your const
holds an object or array, you can still change whatβs inside it.
const user = { name: "Ada" };
user.name = "Lin"; // β
mutating a property is fine
console.log(user.name); // "Lin"
// user = { name: "Other" }; // β but reassigning the binding isn't Weβll come back to this when we cover objects. For now: const says
βthis name will always point at the same thingβ.
What About var?
var was the only way to declare a variable until 2015 (ES6). It has
some surprising behaviors that caused real bugs for two decades. Then
let and const were added to fix them.
You will see var in older code, tutorials, and tweets. But you
shouldnβt use it in new code.
| Keyword | Reassignable | Scope | Notes |
|---|---|---|---|
const | No | Block | β Use by default |
let | Yes | Block | β Use when you need to reassign |
var | Yes | Function | β οΈ Avoid in new code |
Block Scope
let and const are block-scoped: they exist only inside the { }
they were declared in.
{
let message = "inside the block";
console.log(message); // "inside the block"
}
// console.log(message); // β ReferenceError β message doesn't exist out here This is a feature: variables canβt leak out and pollute the rest of
your program. var doesnβt do this β itβs function-scoped, which is
why it caused so many bugs.
Naming Variables
The naming rules are the same ones we covered in JavaScript Syntax:
- Start with a letter,
_, or$. - Then letters, digits,
_, or$. - Canβt be a reserved keyword.
- Case-sensitive.
And one convention: use camelCase.
let userName = "Ada";
let totalPrice = 29.99;
let isLoggedIn = true; Try It Yourself
Exercise
Declare a greeting
// Declare `greeting` here and log it.
π‘ Show hint
β Show solution
const greeting = "hello";
console.log(greeting);
Test Your Knowledge
Quiz
Variables Quiz
Six questions on `let`, `const`, `var`, and block scope.
Up Next
Now you know how to store data. Next, two short lessons that zoom in
on let and const specifically.