Variables

Course 1 · Ch 2
Variables, Data Types, and var/let/const
Storing values with a name, and the three different keywords JavaScript gives you to do it

Chapter 1 used console.log("Hello, JS!") directly on a literal value. Real code almost always needs to store a value first, under a name, so it can be reused and changed — that's what a variable is. JavaScript has three keywords for declaring one: var, let, and const — only two of which are worth reaching for in modern code.

Declaring a Variable

let name = "Philip"; let age = 35; console.log(name); // "Philip" console.log(age); // 35

let name = "Philip"; creates a variable called name, holding the value "Philip". There's no need to declare what type of value it will hold up front — JavaScript figures that out automatically from whatever is assigned.

let vs const — Can It Be Reassigned?

let score = 0; score = 10; // fine — let allows reassignment const pi = 3.14159; pi = 4; // TypeError: Assignment to constant variable

let can be reassigned later; const cannot — trying to assign a new value to a const variable throws an error immediately. The rule of thumb: default to const unless the value genuinely needs to change later, then switch to let. This makes code easier to reason about, since a const guarantees that value never changes anywhere below its declaration.

var still exists, but avoid it
var is the original way to declare a variable, predating let/const (added in 2015). It has looser, more error-prone scoping rules — a var declared inside an if block leaks out into the surrounding function, unlike let/const, which stay neatly contained to the block they're declared in. Modern code has no good reason to use var.

JavaScript's Core Data Types

let count = 42; // number — both whole numbers and decimals let price = 19.99; // number — same type, no separate "float" let label = "Hello"; // string — text, single or double quotes let active = true; // boolean — true or false let nothing = null; // null — deliberately "no value" let notSet; // undefined — declared, but never given a value

Unlike languages with separate integer/decimal types, JavaScript has just one number type covering both. null and undefined are subtly different: undefined means a variable was never assigned anything; null means a value was deliberately set to "nothing" on purpose.

Checking a Value's Type

console.log(typeof 42); // "number" console.log(typeof "hello"); // "string" console.log(typeof true); // "boolean" console.log(typeof undefined); // "undefined"

typeof returns a string naming a value's type — useful while learning, and occasionally in real code when a value's type genuinely isn't known in advance.

Template Literals — Building Strings with Variables

let name = "Philip"; let age = 35; console.log(`Hello, my name is ${name} and I'm ${age}.`); // Hello, my name is Philip and I'm 35.

Backticks (`) instead of quotes create a template literal, allowing ${ } to embed variables (or any expression) directly inside a string — far more readable than joining strings together with +.

KeywordReassignable?Use it when...
constNoThe default choice — value won't change
letYesThe value genuinely needs to change later
varYesAvoid — kept only for reading old code

Coding Challenges

Challenge 1

Declare a const city with your city's name, and a let temperature with a number. Log both using a single template literal in the form "It is X degrees in Y".

📄 View solution
Challenge 2

Declare let score = 0. Reassign it three times (e.g. += 10 each time), logging score after each reassignment. Then declare const maxScore = 100 and try reassigning it, observing the error in the console.

📄 View solution
Challenge 3

Declare one variable of each core type (number, string, boolean, null, undefined) and use typeof to log each variable's type alongside its value.

📄 View solution

Chapter 2 Quick Reference

  • const — cannot be reassigned; the default choice
  • let — can be reassigned; use only when the value will actually change
  • var — older, loosely-scoped; avoid in modern code
  • Core types: number, string, boolean, null, undefined
  • typeof value — returns a string naming that value's type
  • null — deliberately "no value"; undefined — never assigned a value
  • Template literals: `text ${variable} more text` — backticks, not quotes
  • Next chapter: operators and control flow — if/else, switch, ternary