Phase 1 · FoundationsModule 2~40 min read

Variables & Data Types

Store information with const and let, understand JavaScript's value types, and convert data deliberately.

What you'll learn

Programs become useful when they can remember information. JavaScript stores values through named bindings and lets values carry their own types.

  • Choose between const and let
  • Recognize all primitive types and object values
  • Inspect and convert values without accidental coercion
  • Predict copying behavior for primitives and objects

const, let, and legacy var

A variable name is best pictured as a label attached to a value. Use const when the label will not be reassigned and let when it must point to a different value later.

variables.js
const courseName = "JavaScript";
let completedLessons = 0;

completedLessons = completedLessons + 1;
console.log(courseName, completedLessons);
KeywordCan reassign?ScopeUse
constNoBlockDefault choice
letYesBlockValues that change
varYesFunctionLegacy code only

const does not make objects immutable

It prevents reassignment of the binding. Properties inside a const object can still change; later modules show how to design immutable updates.

The seven primitive types

TypeExamplePurpose
string"hello"Text
number42, 3.14, NaNMost numeric work
bigint9007199254740993nArbitrarily large integers
booleantrueLogical state
undefinedundefinedMissing/uninitialized value
symbolSymbol("id")Unique identifiers
nullnullIntentional absence

Primitives are immutable. A string method can produce a new string, but it cannot alter the original string value.

Reference values

Everything else is an object, including arrays and functions. Objects group mutable properties; variables hold references that lead to those objects.

objects.js
const learner = { name: "Lina", level: 1 };
const topics = ["variables", "types"];

learner.level = 2;
topics.push("conversion");
console.log(learner.level, topics.length);

typeof and dynamic typing

JavaScript is dynamically typed: values have types, but a binding can later receive a value of another type. typeof reports a useful string for quick inspection.

types.js
let value = 42;
console.log(typeof value);     // number

value = "forty-two";
console.log(typeof value);     // string
console.log(typeof true);      // boolean
console.log(Array.isArray([])); // true

Two historical surprises

typeof null is "object" because of a decades-old bug. Arrays also report "object", so detect them with Array.isArray(value).

null vs undefined

undefined usually means JavaScript has no value yet: a declared-but-unassigned variable, a missing property, or a function with no return. null is normally written by a programmer to mean “intentionally empty.”

Key idea

Do not use an undeclared name. Reading missingName throws a ReferenceError; reading a declared variable whose value is undefined is valid.

Explicit type conversion

Convert at boundaries—form input, URLs, storage, and APIs—so the rest of your code works with predictable types.

conversion.js
const input = "42";
const quantity = Number(input);

console.log(quantity + 8);
console.log(String(false));
console.log(Boolean(""));
console.log(Number.isNaN(Number("oops")));

Tip

Prefer Number.isNaN(value) to the older global isNaN, which converts its argument before testing.

Copying values vs references

copying.js
let firstScore = 10;
let secondScore = firstScore;
secondScore = 99;

const firstUser = { name: "Sam" };
const secondUser = firstUser;
secondUser.name = "Mira";

console.log(firstScore, secondScore);
console.log(firstUser.name);

Primitive assignment copies the value. Object assignment copies the reference, so both variables reach the same object. A later lesson covers shallow and deep copying.

Recap & quick check

Key takeaways

  • Prefer const; use let only when reassignment is part of the design.
  • JavaScript has seven primitive types; arrays and functions are object values.
  • Dynamic typing means bindings can reference values of different types over time.
  • Convert external data explicitly and remember that object assignment copies a reference.

Quick check

1. Which declaration should be your default?

2. What does typeof null return?

3. Which value represents intentional absence by convention?

4. After const b = a for an object a, what does b contain?