Swipe
SwipeSwipe
Swipe Apps

Paths

Motivation

Information

Declaring Variables · Lesson 8 of 15

let

A free lesson from Swipe Coding · JavaScript, level 2 · updated October 2026

By the Swipe team, founded by Colin · how we make lessons

🤖
Variables are where your code starts remembering things. Let's give it something to hold onto. 📦

A variable is a named container that stores a piece of data your program can work with and change while it runs. In JavaScript, you declare a reassignable variable with the let keyword — and unlike Java, you never declare a type. JavaScript is dynamically typed, so the same variable can hold a number today and something else entirely later.

🤖
Forget to assign a value and JavaScript won't error — it just hands you undefined. 🤷

The general pattern is: let name = value;. You can also declare a variable without giving it a value right away, and assign one later — but if you log it before assigning anything, you'll see the special value undefined rather than an error.

let age = 25;
console.log(age); // 25
age = 26;
console.log(age); // 26
Declaring age with let, then reassigning it to a new value — no need to write let again.

Variable names must start with a letter, an underscore (_), or a dollar sign ($) — never a digit — and cannot be one of JavaScript's reserved keywords (like let or const itself). Names are case-sensitive, so score and Score are two different variables. By convention, JavaScript developers write variable names in camelCase: the first word lowercase, each following word capitalized, e.g. totalScore or firstName.

Fill in the blank

In JavaScript, variable names conventionally use ____, where the first word is lowercase and every following word starts with a capital letter, such as totalScore.

Curly braces {} mark out a block — the body of an if statement, a for loop, a while loop, or a function. A variable declared with let is block-scoped, meaning it only exists inside the nearest enclosing pair of {} where it was declared. Once that block ends, the variable is gone.

if (true) {
  let message = "Hello!";
  console.log(message); // "Hello!"
}
console.log(message); // ReferenceError: message is not defined
message only exists inside the if block — trying to use it afterward throws an error.
Quick check

What happens if you try to use a variable declared with let outside the block {} where it was declared?

True or false

Once you declare a variable with let, you must write the word let again every time you give it a new value.

Keep your streak going in Swipe Coding

Swipe through every level of JavaScript on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.

Get the Swipe apps →