Coding/Learn/javascript/variables and types
JavaScript Foundational Course
01.Introduction to JavaScript & V8 Engine
02.Console Output & Program Flow
03.Variables & Data Types (let, const, typeof)
04.Template Literals & String Interpolation
05.Type Coercion & Strict Equality (=== vs ==)
06.Arithmetic, Math Object & Modulo
07.Conditionals & If / Else Branching
08.Logical Operators & Nullish Coalescing (??)
09.Ternary Operator & Switch Statements
10.While & Do-While Loops
11.Standard For Loops & Iteration
12.Loop Control (Break & Continue)
13.Functions, Arrow Syntax & Return Values
14.Default Parameters, Rest & Spread Syntax
15.Arrays, Indexing & Mutating Methods
16.Array Iterators (map, filter, reduce)
17.Objects, Key-Value Pairs & Destructuring
18.Scope, Lexical Environment & Closures
19.Asynchronous JavaScript (Promises & Async/Await)
20.Capstone: Interactive E-Commerce Cart & Ledger Engine
Course Certificate0%

Complete all lessons to earn

LearnVariables & Data Types (let, const, typeof)
3 / 20 0%
Lesson 03 of 20
6 mins read

Variables & Data Types (let, const, typeof)

PreviousNext Lesson
In Plain English

JavaScript features 7 primitive types: number, string, boolean, null, undefined, symbol, and bigint. Variables are declared with const (immutable binding) or let (reassignable), while typeof inspects the runtime type.

The Mental Model

"A variable is a labeled container. const is a welded box whose contents cannot be swapped for a new item, whereas let is an open jar where you can place different values as needed."

Deep Dive: How It Works

const vs let: Use const by default. Use let only when you know the variable will be reassigned (like loop counters).

Numbers: All numbers in JavaScript are IEEE 754 double-precision floats (no separate int vs float type for standard arithmetic).

null vs undefined: undefined means a variable has been declared but not assigned. null is an intentional empty value.

Syntax Blueprint

const name = "Ada"; let score = 100; typeof score ──▶ "number"

Use const for immutable bindings, let for reassignments, and typeof to inspect data types.

PointCore Rules to Remember

Point star
let: Block-scoped, reassignable variable declaration.
Point star
const: Block-scoped, non-reassignable constant reference.
Point star
typeof Operator: Returns a string indicating the data type (e.g., "string", "number", "boolean").

Common Beginner Traps & How to Fix Them

Mistake:Attempting to reassign a const variable.

Why it happens: const prevents reassignment to a new value: TypeError: Assignment to constant variable.

How to fix: If a variable must change its value over time, declare it with let instead.

Pro Tip: typeof null returns "object"—this is a historic 1995 JavaScript bug that cannot be changed for backwards compatibility.

Live Interactive Example

Hit Run Code to see it live
Inspecting Types
JavaScript ES2024
1
2
3
4
5
6
7
8
9
10
11
Output Console
Click "Run Code" to view the rendered output.
How it works: Demonstrates JavaScript primitive types and the output of typeof.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Declare variables and inspect type

  • PointDeclare const language = "JavaScript";
  • PointDeclare let activeUsers = 42000;
  • PointPrint "Type of language: string" using typeof language.
  • PointPrint "Type of activeUsers: number" using typeof activeUsers.
1
2
3
4
5
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates