Data types
Why it matters
Values come in different types: text, numbers, "yes/no". The type decides what you can do with a
value. 5 + 5 is 10, but "5" + "5" is "55". Data from forms, files and the internet almost always
arrives as strings, and you must be able to turn them into numbers — otherwise your calculator will
join digits instead of adding them.
The main types
| Type | Examples | What it is |
|---|---|---|
string |
"Anna", '42', `hello` |
a string, text |
number |
42, 3.14, -7 |
a number (integers and decimals are one type) |
boolean |
true, false |
logical "yes / no" |
undefined |
undefined |
"no value yet" |
null |
null |
"no value, on purpose" |
There are also bigint and symbol — they're rarely needed. Arrays and objects come later.
Finding the type: typeof
console.log(typeof "hello");
console.log(typeof 42);
console.log(typeof true);
console.log(typeof undefined);
Output:
string
number
boolean
undefined
A quirk of the language: typeof null gives "object". It's an old bug that can no longer be fixed.
undefined and null
If you create a variable and put nothing in it, it holds undefined:
let answer;
console.log(answer);
Output:
undefined
null is set by you when you want to say "it's empty here": let winner = null; — no winner yet.
Template strings
A string in backticks ` is called a template string. You can insert a value into it with ${...}:
const name = "Anna";
const age = 15;
console.log(`My name is ${name}, I am ${age}`);
console.log(`Next year I'll be ${age + 1}`);
Output:
My name is Anna, I am 15
Next year I'll be 16
Any expression can go inside ${}. It's more convenient than joining pieces with +.
Type conversion
To a number:
console.log(Number("42")); // 42
console.log(Number("3.5")); // 3.5
console.log(Number("")); // 0
console.log(Number("12px")); // NaN
console.log(parseInt("12px")); // 12
console.log(parseFloat("1.5kg")); // 1.5
NaN (Not a Number) is a special numeric value: the result of a failed conversion. You check for it
with Number.isNaN(x). A normal comparison doesn't work: NaN === NaN is false!
Number requires the whole string to be a number. parseInt and parseFloat read digits from the
start of the string and stop at the first extra character.
To a string: String(42) → "42", or `${42}`.
To a boolean: Boolean(x). Values that turn into false are called "falsy":
0, "" (empty string), null, undefined, NaN, false. Everything else is true,
even the string "0" and the string "false".
Automatic conversion
JavaScript sometimes changes types by itself — and that's a source of surprises:
console.log("5" + 2); // 52 — plus with a string joins
console.log("5" - 2); // 3 — minus turns the string into a number
console.log("5" * "2"); // 10
console.log(true + 1); // 2 — true becomes 1
Output:
52
3
10
2
Rule: if either side of + is a string, it joins. So it's better to convert form data to numbers
explicitly with Number(...).
How it works inside
Every value in memory is stored together with a label of its type. When you write a + b, the engine
first looks at the types: two numbers — it adds them; a string — it turns the other part into a string
and joins. Types are not tied to a variable: the same let can hold first a number and then a string.
Such a language is called dynamically typed.
Common mistakes
- Joining instead of adding.
"10" + 5→"105". Fix:Number("10") + 5. Numberon a string with letters.Number("12px")→NaN. You needparseInt("12px").- Comparing with
NaN.x === NaNis alwaysfalse. UseNumber.isNaN(x). - Normal quotes instead of backticks.
"Hello, ${name}"prints the text as is, with the dollar and braces. Insertion only works inside`…`. - The string
"false"is truthy.Boolean("false")→true, because the string isn't empty.
Cheat sheet
| What | How | Result |
|---|---|---|
| Get the type | typeof x |
"string", "number"… |
| String → number | Number("42") |
42 |
| Number from the start | parseInt("12px") |
12 |
| Decimal from the start | parseFloat("1.5kg") |
1.5 |
| Check for NaN | Number.isNaN(x) |
true / false |
| Number → string | String(42) |
"42" |
| Insertion | `Total: ${sum}` |
"Total: 12" |
| Falsy values | 0 "" null undefined NaN false |
everything else is truthy |