JavaScript Tutorial
JavaScript Data Types
string, number, boolean, undefined, null, object, bigint, and symbol. typeof reports the kind.
Eight kinds of value
Every value in JavaScript has a type. The seven primitives are string, number,boolean, undefined, null, bigint, andsymbol. Everything else is an object: plain objects, arrays, dates, and functions.
You do not declare the type when you write let name = "Mina". JavaScript infers it from the value. The same name can later hold a different type. That flexibility is convenient and is also a source of bugs — which is why typeof exists.
typeof
typeof value returns a string naming the kind. Use it when a value is not what you expected — a form field that is still a string, a missing property that is undefined, a function you thought was an object.
Example
console.log(typeof "Mina");
console.log(typeof 42);
console.log(typeof 3.14);
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof { city: "Lisbon" });
console.log(typeof [1, 2, 3]);
Open /javascript/try with Try it in JavaScript. The console prints each type string.
string, number, boolean
A string is text in quotes: "Hello", 'Hello', or a backtick template. A number is an integer or a fraction: 7, 3.5,-2. A boolean is true or false — the result of a comparison, or a flag you set yourself.
These three are the everyday types. Most of the scripts in this tutorial are strings for labels, numbers for counts and prices, and booleans for decisions.
undefined and null
undefined means no value was assigned. A declared let with no initializer isundefined. A missing object property is undefined. A function with noreturn returns undefined.
null is a value you assign on purpose to mean “nothing here.” Empty a selection withactiveUser = null. Do not use undefined for that: let the language useundefined for missing, and use null for empty.
Example
let title;
console.log(title);
console.log(typeof title);
let activeUser = null;
console.log(activeUser);
console.log(typeof activeUser);
typeof null is "object". That is a long-standing language bug, not a rule you should rely on. To test for null, write value === null. Do not use typeof for that check.
object (and arrays, and functions)
An object is a collection of named values: { name: "Mina", age: 28 }. An array is an ordered list: [10, 20, 30]. Both have typeof equal to "object". A function is a callable object; typeof reports "function" so you can tell it apart.
Example
const person = { name: "Mina", city: "Lisbon" };
const scores = [10, 8, 9];
function greet() {
return "hello";
}
console.log(typeof person);
console.log(typeof scores);
console.log(typeof greet);
console.log(Array.isArray(scores));
console.log(Array.isArray(person));
To tell an array from a plain object, use Array.isArray. Do not use typeof for that — both are "object".
bigint and symbol
bigint holds integers bigger than Number.MAX_SAFE_INTEGER. Write an suffix: 9007199254740993n. You cannot mix bigint and number in arithmetic without converting. Everyday pages almost never need bigint.
symbol is a unique identifier: Symbol("id"). Two symbols with the same description are still not equal. Libraries use them as collision-free keys. You can ignore symbols until a later API requires one.
Example
const huge = 9007199254740993n;
console.log(typeof huge);
const first = Symbol("id");
const second = Symbol("id");
console.log(typeof first);
console.log(first === second);
typeof cheat sheet
| Value | typeof |
|---|---|
"Mina" | "string" |
42 | "number" |
true | "boolean" |
undefined | "undefined" |
null | "object" (quirk) |
{} | "object" |
[] | "object" |
function() {} | "function" |
10n | "bigint" |
Symbol("x") | "symbol" |
Types move around
JavaScript converts types when an operator asks for it. "5" * 2 becomes 10."5" + 2 becomes "52". Boolean("") is false. Prefer explicit conversion when the value comes from a form or from JSON: Number(text),String(n), Boolean(value).
Next: functions — a named block you call, with parameters in and a return value out.