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
constandlet - 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.
const courseName = "JavaScript";
let completedLessons = 0;
completedLessons = completedLessons + 1;
console.log(courseName, completedLessons);| Keyword | Can reassign? | Scope | Use |
|---|---|---|---|
const | No | Block | Default choice |
let | Yes | Block | Values that change |
var | Yes | Function | Legacy code only |
const does not make objects immutable
The seven primitive types
| Type | Example | Purpose |
|---|---|---|
| string | "hello" | Text |
| number | 42, 3.14, NaN | Most numeric work |
| bigint | 9007199254740993n | Arbitrarily large integers |
| boolean | true | Logical state |
| undefined | undefined | Missing/uninitialized value |
| symbol | Symbol("id") | Unique identifiers |
| null | null | Intentional 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.
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.
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([])); // trueTwo 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
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.
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
Number.isNaN(value) to the older global isNaN, which converts its argument before testing.Copying values vs references
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?