Basic

Variables and Types

Store and name data using let, const, and understand the core built-in types.

What you will see

99

About 3 min of reading

Try It

Declare variables with const and let, then watch JavaScript store each type and print it back. Reassign a value, build a string with a template literal, and see why an object stays linked even when you copy its name.

1

Create a constant and a mutable variable, print them together, then ask typeof for the number. Notice how each value prints exactly the way you stored it.

const name = "Ada"; let age = 30; console.log(name, age); console.log(typeof age);

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Ada 30 number
`const` freezes the binding so you cannot reassign name later; reach for `let` only when a value genuinely changes. Prefer `const` by default.
2

Build a sentence with a template literal, embedding a direct expression inside ${...}. The backticks let the expression calculate itself into the text.

const name = "Ada"; const age = 30; const line = `${name} will turn ${age + 1} next year`; console.log(line);

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Ada will turn 31 next year
Template literals use backticks and ${...} interpolation. Inside the braces you can put any expression, including a function call.
3

Copy an object by name and change a property on the copy. Print the original to see it changed too, because objects are copied by reference, not by value.

const a = { x: 1 }; const b = a; b.x = 99; console.log(a.x);

Before you run it, say out loud what it will print. Then open the answer.

What it prints
99
Primitives copy by value; objects, arrays, and functions copy by reference. Spread with `{ ...a }` to break the link.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

A value is created, bound to a name, and later read back through that name.
1 Write a literal (e.g. 30) -> runtime stores it
↓
2 Bind the name with const/let -> the name points to that value
↓
3 Read the name later -> runtime follows the reference
↓
4 Convert or inspect -> typeof or constructor changes the form
Why this works
Why const by default

Most bugs come from state changing when you don't expect it. const forbids reassignment, which makes that class of bug impossible. A const array or object can still be mutated (its contents change), but the binding itself is frozen:

const nums = [1, 2, 3];
nums.push(4);       // allowed: mutating contents
nums = [5];         // TypeError: Assignment to constant variable.

Reach for let only when a variable genuinely needs a new binding, like a loop counter.

When you have a choice

Primitive vs reference values

Primitives (number, string, boolean, bigint, symbol, null, undefined) are copied by value. Objects, arrays, and functions are copied by reference (a pointer to the same underlying data):

const a = { x: 1 };
const b = a;        // b points to the SAME object
b.x = 99;
console.log(a.x);   // 99 (a changed too!)

This matters when passing objects to functions: the function can mutate the caller's object. Copy with { ...a } or [...a] to break the link.

Examples

Declaring variables with let and const

Declaring variables

JavaScript has three ways to declare a variable:

const name = "Ada";
let age = 30;
var legacy = "old";

const cannot be reassigned; let can; var is the old global-scope form. Prefer const by default, only use let when you know the value will change. Avoid var.

Why it works: const binds a name to a value for the rest of its scope, so the runtime throws if you try to reassign it.

The primitive types

The core types

console.log(typeof 42);          // number
console.log(typeof "hi");        // string
console.log(typeof true);        // boolean
console.log(typeof undefined);   // undefined
console.log(typeof Symbol());    // symbol

number, string, boolean, undefined, symbol, and (since ES2020) bigint are primitives. typeof null returns "object": a historical bug that is kept for compatibility.

Strings and template literals

Building strings

const name = "Ada";
const age = 30;
const line = `${name} will turn ${age + 1} next year`;
console.log(line);   // Ada will turn 31 next year

Template literals use backticks and ${...} to embed expressions directly. They support newlines and multi-line text without escape characters.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

Next Lesson
Strings and Text Operations
Strings and Text Operations →