1. Values & Types
Every piece of data in JavaScript is a value, and every value has
a type. JavaScript has seven primitive types, plus object
for everything else (arrays, functions and plain objects are all, under the hood,
objects):
typeof 42; // "number"
typeof 3.14; // "number" -- JS has one numeric type, no int/float split
typeof "hello"; // "string"
typeof true; // "boolean"
typeof undefined; // "undefined" -- a variable that was declared but never assigned
typeof null; // "object" -- a decades-old bug in the language, kept for compatibility
typeof Symbol(); // "symbol" -- rare in everyday code, skip for now
typeof 10n; // "bigint" -- integers larger than Number can safely hold
null reporting as "object" is one of JavaScript's oldest
known bugs — it can never be fixed without breaking the web, so it's just something
to memorize. In practice, null means "intentionally no value" and
undefined means "no value has been assigned yet" — a subtle but real
distinction worth holding onto.
Unlike languages with separate int and float types, every number in JavaScript — 1, 1.5, -3 — is the same number type, stored as a 64-bit floating-point value. This explains why 0.1 + 0.2 doesn't print exactly 0.3; it's a floating-point rounding quirk shared by nearly every language that uses this same number format, not a JavaScript-specific bug.
2. let, const & var
Variables are declared with one of three keywords — but only two of them belong in code you write today:
const name = "Ada"; // can't be reassigned -- the default choice
let age = 28; // can be reassigned -- use only when a value truly changes
age = 29; // fine, `age` was declared with `let`
var legacy = "avoid me"; // works, but has confusing scoping rules -- covered next week
Default to const for every variable, and reach for
let only when you specifically need to reassign it later (a loop
counter, a running total). var still works — huge amounts of existing
code use it — but its scoping behavior is genuinely confusing (full explanation
next week) and there is no situation in modern code where var is the
better choice over let or const.
const doesn't mean "immutable"
const only prevents reassigning the variable itself — an object or array stored in a const can still have its contents changed. const obj = { a: 1 }; obj.a = 2; works fine. Only obj = { a: 3 }; (reassigning the whole variable) throws an error.
3. Operators & Type Coercion
JavaScript's arithmetic and comparison operators mostly look like every other C-family language, with one significant exception worth learning correctly from day one:
5 == "5"; // true -- coerces types before comparing, surprising results
5 === "5"; // false -- compares value AND type, no coercion
null == undefined; // true
null === undefined; // false
// Always use === and !== in real code
== (loose equality) silently converts one or both operands to try to
make them comparable, which produces genuinely surprising results
("" == 0 is true; [] == false is
true). === (strict equality) never coerces, so it does
exactly what it looks like it does. Use === and
!== everywhere — there is essentially no real code written
today that intentionally relies on =='s coercion.
10 + 3; // 13
10 - 3; // 7
10 * 3; // 30
10 / 3; // 3.3333333333333335
10 % 3; // 1 -- remainder
10 ** 3; // 1000 -- exponent
true && false; // false -- AND
true || false; // true -- OR
!true; // false -- NOT
4. Template Literals
Template literals (backtick strings) let you embed expressions directly inside a string, and write multi-line strings without special escape sequences:
const name = "Ada";
const age = 28;
// Old way -- string concatenation
const oldGreeting = "Hello, " + name + "! You are " + age + " years old.";
// Template literal -- clearer, and handles multi-line naturally
const greeting = `Hello, ${name}! You are ${age} years old.`;
const multiLine = `Line one.
Line two.
Line three.`;
Anything inside ${'{'}...{'}'} is a real JavaScript expression, not
just a variable name — ${'{'}age + 1{'}'} or ${'{'}name.toUpperCase(){'}'}
both work exactly as you'd expect.
5. Control Flow
Two constructs cover almost every branching decision a program needs to make:
const hour = 14;
if (hour < 12) {
console.log("Good morning");
} else if (hour < 18) {
console.log("Good afternoon");
} else {
console.log("Good evening");
}
const day = "Tue";
switch (day) {
case "Mon":
case "Tue":
case "Wed":
case "Thu":
case "Fri":
console.log("Weekday");
break;
case "Sat":
case "Sun":
console.log("Weekend");
break;
default:
console.log("Not a valid day");
}
switch is worth reaching for when you're comparing one value against
several possible exact matches — it reads more clearly than a long chain of
else if checks doing the same job. Don't forget break on
each case; without it, execution "falls through" into the next case, which is
occasionally useful (as with "Mon" through "Fri" above)
but usually a bug.
6. Hands-on Exercise
Write a small temperature-conversion script
Apply this week's core building blocks to a real, working program.
Requirements:
- Create a file
convert.js. Declare aconst celsiusholding a number of your choice. - Write the Fahrenheit conversion formula (
F = C * 9/5 + 32) into aconst fahrenheit. - Use a template literal to log a sentence like
"25°C is 77°F"— don't use string concatenation with+. - Add an
if/else if/elsechain that logs"freezing","cold","mild", or"hot"based on the Celsius value, using ranges of your choice. - Run it with
node convert.jsand confirm the output is correct for at least three different starting temperatures.
If node convert.js says "command not found," Node.js isn't installed yet — download the LTS version from nodejs.org and confirm with node --version in your terminal.
7. Knowledge Check
Four quick questions. Expand each to check your answer.
Q1
Why does typeof null return "object"?
Why does typeof null return "object"?
It's a long-standing bug from JavaScript's original 1995 implementation. It can't be fixed now without breaking an enormous amount of existing code across the web, so it has simply been kept as-is. In practice, treat null as its own special "intentionally no value" case rather than trusting typeof to identify it.
Q2
Does const obj = { a: 1 }; obj.a = 2; throw an error?
Does const obj = { a: 1 }; obj.a = 2; throw an error?
No — it works fine. const only prevents the variable obj itself from being reassigned to point at a different value; it says nothing about whether the object's contents can change. Only something like obj = { a: 3 }; (reassigning the whole variable) would throw.
Q3
Why does this course recommend === over ==?
Why does this course recommend === over ==?
== silently converts operands to try to make them comparable before checking equality, which produces surprising results like "" == 0 being true. === never coerces — it checks both value and type — so it behaves exactly the way it reads. There's essentially no situation in modern code where relying on =='s coercion is the clearer choice.
Q4
What happens if you forget break inside a switch case?
What happens if you forget break inside a switch case?
Execution "falls through" into the next case and keeps running its code too, regardless of whether that case's value actually matched. This is occasionally used deliberately (grouping several case labels to share one block, as with the weekday example above) but is a common source of bugs when it's accidental — always add break unless the fall-through is intentional.