beginner20 min

Variables and Data Types

Store and label values with let and const, and meet JavaScript's basic types.

What you'll learn

  • Declare variables with let and const and explain when to use each
  • Identify JavaScript's basic primitive types
  • Use typeof to inspect a value's type

Explanation

A variable is a named box that holds a value. In modern JavaScript you create one with let (for a value that may change later) or const (for a value that should never be reassigned). Avoid the older var keyword — it has confusing scoping rules that let/const were introduced to fix.

let score = 0;
score = score + 10; // fine — let allows reassignment

const name = "Ada";
// name = "Grace"; // error — const cannot be reassigned

const doesn't mean "unchangeable data" — it means "this variable name can't be pointed at a new value." An array or object stored in a const can still have its contents modified; only the variable binding itself is frozen.

JavaScript has a handful of basic (primitive) types you'll use constantly:

  • string — text, written in quotes: "hello"
  • number — both integers and decimals share one type: 42, 3.14
  • boolean — exactly true or false
  • undefined — a variable that has been declared but never given a value
  • null — an intentional "no value," set explicitly by your code

You can check a value's type at runtime with the typeof operator, which returns a string like "string" or "number". This is useful when debugging unexpected behavior — a shockingly large number of JavaScript bugs come down to a value being a different type than you assumed (famously, typeof null returns "object", a long-standing quirk worth just memorizing).

Naming variables well is a skill in itself: prefer descriptive names like itemCount over vague ones like x, since you and other readers will meet that name again far from where it was declared.

Example

Declaring variables of different types and inspecting them with typeof.

let itemCount = 3;
const storeName = "Corner Bookshop";
let isOpen = true;
let discount; // undefined until assigned

console.log(typeof itemCount);
console.log(typeof storeName);
console.log(typeof isOpen);
console.log(typeof discount);

Try it yourself

Add your own variable of each type, then log its typeof.

Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.

Loading editor…

Guided exercise

Guided exercise

Declare a const named `city` set to any city name, and a let named `population` set to any number. Then log both variables.

Checks: city is a string · population is a number

Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.

Loading editor…

Stuck? Get a hint.

Independent exercise

Independent exercise

Declare four variables named exactly `title` (string), `price` (number), `inStock` (boolean), and `notes` (left as undefined, i.e. declared with let but never assigned).

Checks: title is a string · price is a number · inStock is a boolean · plus 1 hidden check

Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.

Loading editor…

Stuck? Get a hint.

Common mistakes

  • Using var out of old habit — it can silently leak variables outside the block you intended.
  • Wrapping numbers in quotes (making them strings) by accident, e.g. `let price = "9.99"`.
  • Assuming const freezes an object's contents — it only prevents reassigning the variable itself.

Knowledge check

Knowledge check

1. Which declaration should you use for a variable you will never reassign?
2. What does `typeof "hello"` return?
3. What is the value of a variable declared with `let x;` and never assigned?

Takeaway

let and const name your data; typeof lets you check what kind of data you actually have.

Summary

Variables are declared with let (reassignable) or const (fixed binding). JavaScript's basic types include string, number, boolean, undefined, and null, and typeof reports which one a value currently is.

References

Your notes

Notes save automatically.

Finished this lesson?

Mark it complete to track your progress and schedule a future review.