JavaScript Variables

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.

7 min read Level 1/5 #variables#let#const
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.

Your first variable script.js
let greeting = "Hello, world!";
console.log(greeting);
β–Ά Preview: console

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, const, and var script.js
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);
β–Ά Preview: console

const vs let

const declares a value that cannot be reassigned. let declares one that can. That’s the only difference.

const can't be reassigned script.js
const PI = 3.14159;
// PI = 3;  // ❌ TypeError: Assignment to constant variable.

let count = 0;
count = 1;  // βœ… fine β€” let allows reassignment
console.log(count);
β–Ά Preview: console

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 objects can still mutate script.js
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
β–Ά Preview: console

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.

KeywordReassignableScopeNotes
constNoBlockβœ… Use by default
letYesBlockβœ… Use when you need to reassign
varYesFunction⚠️ Avoid in new code

Block Scope

let and const are block-scoped: they exist only inside the { } they were declared in.

Block scope script.js
{
  let message = "inside the block";
  console.log(message);  // "inside the block"
}

// console.log(message);  // ❌ ReferenceError β€” message doesn't exist out here
β–Ά Preview: console

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

Difficulty 1/5~2 min
Declare a constant called `greeting` and assign it the string `"hello"`. Then `console.log(greeting)` to print it.
solution.js
// Declare `greeting` here and log it.
4tests will run
πŸ’‘ Show hint
Use the `const` keyword. Strings in JavaScript can be written with either single or double quotes.
βœ… Show solution
const greeting = "hello";
console.log(greeting);

Test Your Knowledge

Quiz

Variables Quiz

Six questions on `let`, `const`, `var`, and block scope.

0/ 8
  1. Question 1
    1

    Which keyword declares a value that cannot be reassigned?

  2. Question 2
    2

    Which of these declarations are **block-scoped**?

    Select all that apply.

  3. Question 3
    3

    const objects are deeply immutable β€” you cannot change their properties.

  4. Question 4
    4

    The keyword used to declare a value that won't change is _____.

  5. Question 5
    5

    What does this code log?

    const x = 5;
    x = 10;
    console.log(x);
    
  6. Question 6
    6

    Match each keyword to its scope.

    let
    const
    var
Pick an answer β€” instant feedback as you go.

Up Next

Now you know how to store data. Next, two short lessons that zoom in on let and const specifically.

JavaScript let β†’
Reference