Data Types
Values Have Kinds
In the previous lesson you stored values in boxes. But not all values are the same kind of thing.
The number 42 and the text "42" look similar to a human eye. To a computer, they are completely different objects stored differently in memory, with different operations permitted on them. Trying to use one where the other is expected is one of the most common sources of bugs in all of programming.
JavaScript has 7 primitive types. You will use 5 of them constantly. Let's meet them one by one.
Number
In JavaScript, there is only one type for all numbers. Integers, decimals, negative — they are all just number.
The % operator — modulo — deserves special attention. It gives you the remainder after division. 10 % 3 is 1 because 10 divided by 3 is 3 with a remainder of 1. You will use modulo constantly: to check if a number is even or odd, to wrap values around a range, to build cycles.
The Strange Number: NaN
NaN stands for Not a Number. It is JavaScript's way of saying: "You asked me to do arithmetic on something that isn't a number, so the result is meaningless."
The bizarre thing: typeof NaN returns "number". NaN is a number-type value that represents an invalid number. It is the only value in JavaScript that is not equal to itself — NaN === NaN is false. This surprises everyone the first time.
String
A string is a sequence of characters — any text at all, wrapped in quotes.
The backtick syntax `${greeting}, ${name}!` is called a template literal. It lets you embed variables directly inside a string using ${}. This is almost always cleaner than using + to join strings together.
name[0] accesses the first character. Strings in JavaScript are zero-indexed — the first character is at position 0, not 1. This confuses beginners exactly once, after which it becomes second nature.
Strings Are Immutable
Once a string exists, it cannot be changed. You can create a new string based on an old one, but you cannot modify the original.
Boolean
A boolean has exactly two possible values: true or false. Nothing else.
Notice === — three equals signs. This is strict equality: the values must be the same and the same type. 10 === "10" is false because one is a number and the other is a string.
There is also == (two equals signs), which does type coercion before comparing. 10 == "10" returns true. Avoid ==. It produces surprises. Always use ===. The extra = costs nothing and saves hours of debugging.
Undefined
undefined means: a variable exists, but has never been given a value.
undefined is not something you typically assign on purpose. It is the computer's way of saying: "This box exists but no one has put anything in it yet."
When you call a function without providing an expected argument, that argument is undefined inside the function. This is JavaScript being permissive — it does not crash, it just proceeds with undefined. Whether that is helpful or dangerous depends entirely on the situation.
Null
null means: this variable intentionally has no value.
Run it. typeof null returns "object". This is a bug in JavaScript that has existed since 1995 and cannot be fixed without breaking the internet. Every JavaScript programmer knows it. Accept it and move on.
Null vs Undefined — The Real Distinction
This is the thing most tutorials explain poorly. Here it is clearly:
| Meaning | Who sets it | |
|---|---|---|
undefined | Value was never assigned | JavaScript itself |
null | Value is intentionally absent | You, the programmer |
undefined is JavaScript talking. null is you talking.
When you retrieve a user from a database and no user exists, you return null — because you are deliberately communicating "there is no user here." When you declare let x without assigning it, JavaScript sets it to undefined — because you said nothing about its value.
Checking Types with typeof
The typeof operator tells you what type a value is:
Notice that arrays and objects both return "object". typeof is useful for distinguishing primitives, but not detailed enough for complex values. You will learn better tools for that later.
Type Coercion — The Full Picture
In Lesson 1 you saw that 10 + "20" gives "1020". Now you understand why: JavaScript sees a number and a string, decides to convert the number to a string, and concatenates them. This automatic conversion is type coercion.
+ is the dangerous one because it serves double duty: addition for numbers, concatenation for strings. When in doubt, use explicit conversion:
What You Have Learned
Every value in JavaScript has a type. The five you will use daily:
- Number — all numeric values;
NaNis its strange edge case - String — text; zero-indexed; immutable; use template literals
- Boolean —
trueorfalse; always use===not== - Undefined — JavaScript's signal that a value was never assigned
- Null — your signal that a value is intentionally absent
Type coercion happens automatically and silently. Knowing when and why it happens is the difference between a programmer who writes reliable code and one who spends hours chasing phantom bugs.
In the next lesson, we meet conditionals — how a program makes decisions. This is where code stops being a sequence of instructions and starts feeling like it can think.