Data Types
The value "250" and the value 250 look almost the same, but JavaScript treats them very differently. Add 50 to the first and you get "25050"; add 50 to the second and you get 300. The difference is their data type, the kind of value they are.
In this lesson you will meet all eight data types in JavaScript, learn to check a value's type with typeof, and convert values from one type to another without surprises.
The eight data types
Every value in JavaScript belongs to one of eight types. Seven of them are primitive types: simple, single values that cannot be changed. The eighth, object, holds collections of values.
| Type | What it holds | Example |
|---|---|---|
| string | Text | "Kochi", 'Isha' |
| number | Whole and decimal numbers | 42, 3.5, -7 |
| bigint | Very large whole numbers | 9007199254740993n |
| boolean | Yes or no | true, false |
| undefined | A variable with no value yet | undefined |
| null | "No value", set on purpose | null |
| symbol | A unique identifier | Symbol("id") |
| object | Collections and everything else | { name: "Farhan" }, [1, 2, 3] |
You will use strings, numbers, booleans, null, undefined and objects every day. BigInt is for numbers larger than about nine quadrillion, which you will see in Numbers & Math. Symbols are used in advanced code and libraries, and you can ignore them for now.
Arrays, functions and dates are not separate types: they are all special kinds of object.
Checking a type with typeof
The typeof operator tells you the type of a value as a string.
console.log(typeof "Kochi");
console.log(typeof 42);
console.log(typeof 3.5);
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof 10n);
console.log(typeof { name: "Farhan" });
console.log(typeof null);string number number boolean undefined bigint object object
Notice two things. First, there is only one number type: whole numbers and decimals are both number. Second, typeof null says "object". This is a bug from the very first version of JavaScript that can never be fixed, because fixing it would break old websites. To check for null, compare directly: value === null.
typeof has one more quirk: for an array it also says "object". To check whether a value is an array, use Array.isArray(value). For a function, typeof gives "function", which is handy.
undefined and null
Both mean "no value", but they are used differently:
undefinedusually means JavaScript has not been given a value: a variable declared without one, a missing function argument, or an object property that does not exist.nullmeans you, the programmer, decided there is no value, for example a user who has not chosen a delivery slot.
let deliverySlot; // not set yet
const middleName = null; // set on purpose: this person has none
console.log(deliverySlot);
console.log(middleName);undefined null
A good habit: never assign undefined yourself. Use null when you want to say "empty on purpose", and let undefined mean "not set". The operators in Nullish Coalescing & Optional Chaining handle both neatly.
Dynamic typing
JavaScript is dynamically typed: a variable does not have a fixed type, only its current value does. The same let variable can hold a number now and a string later. This is flexible, but it also means mistakes are not caught until the code runs, so keep each variable to one type. (TypeScript, a popular language built on JavaScript, adds fixed types for exactly this reason.)
Primitives are copied, objects are shared
When you copy a primitive into another variable, you get an independent copy. When you copy an object, both names point to the same object.
let a = 10;
let b = a;
b = 20;
console.log(a, b);
const order1 = { item: "Samosa", qty: 2 };
const order2 = order1;
order2.qty = 5;
console.log(order1.qty, order2.qty);10 20 5 5
Changing b did not touch a. But order1 and order2 are two names for one object, so a change through either name shows up in both. You will learn how to make real copies of objects in Destructuring, Rest & Spread.
Converting between types
Values often arrive as the wrong type. Text typed into a form field is always a string, even if the user typed digits. Convert it yourself with Number(), String() and Boolean().
const fromForm = "250"; // form values are always strings
console.log(fromForm + 50);
console.log(Number(fromForm) + 50);
console.log("8" - 3);
console.log(String(99) + 1);
console.log(Number("12 kg"));
console.log(Boolean(""), Boolean("hello"));25050 300 5 991 NaN false true
Read each line carefully:
+with a string joins text, so"250" + 50gives"25050".- After
Number(), it is a real sum:300. -only works on numbers, so JavaScript quietly converted"8"to8. This silent conversion is called type coercion.Number("12 kg")cannot make a number and givesNaN, which means "Not a Number".- An empty string converts to
false; any other string converts totrue.
Rely on explicit conversion, not coercion. It makes your intent clear and avoids surprises. More conversion tools, such as parseInt(), are covered in Numbers & Math.
Common mistakes
- Adding form input without converting.
"500" + "250"is"500250". UseNumber()first. - Checking for null with
typeof. It returns"object". Usevalue === null. - Checking for arrays with
typeof. It also returns"object". UseArray.isArray(). - Thinking a copied object is separate.
const b = awith an object shares it; it does not copy it.
Key points
- JavaScript has eight types: string, number, bigint, boolean, undefined, null, symbol and object.
typeofreturns a value's type as a string;typeof nullis"object"for historical reasons.undefinedmeans "not set";nullmeans "empty on purpose".- Variables can change type, but keeping one type per variable avoids bugs.
- Primitives are copied; objects are shared between variables.
- Convert types explicitly with
Number(),String()andBoolean().
Exercise
A shop form in Jaipur gives you three strings: "3" (quantity), "149" (price) and "yes" (gift wrap). Convert the quantity and price to numbers and print the total. Then print the type of the total with typeof, and print what Number("yes") gives.
Show solution
const quantity = Number("3");
const price = Number("149");
const total = quantity * price;
console.log("Total:", total);
console.log(typeof total);
console.log(Number("yes"));Total: 447 number NaN
After conversion the values are numbers, so the multiplication works and the total is a number. Text that is not a number becomes NaN.
Quick Quiz
1. What does typeof null return?
This is a long-standing bug in JavaScript. Use value === null to test for null.
2. What is the result of "20" + 5?
When one side of + is a string, JavaScript joins the values as text.
3. How should you check whether a value is an array?
typeof says "object" for arrays, so use Array.isArray().
4. After const a = { n: 1 }; const b = a; b.n = 2;, what is a.n?
a and b point to the same object, so the change made through b is visible through a.
Learn JavaScript with a mentor
Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
