JavaScript Data Types
Table of Contents + −
In the previous lesson, we learned how to declare variables. Now let’s look at the different kinds of values, called data types, that those variables can hold.
🔢 What are Data Types?
A data type describes the kind of value you are working with. A piece of text and a number are different types, and JavaScript treats them differently.
JavaScript values fall into two groups:
- Primitive types 🧱 - simple, single values like text, numbers, and true/false
- Objects 📦 - collections of values, like arrays and objects
🧱 Primitive Data Types
These are the basic building blocks. Here are the primitive types you will use most:
| Type | Description | Example |
| String | Text, written inside quotes | "Hello" |
| Number | Any number, whole or decimal | 42, 3.14 |
| Boolean | A true or false value | true, false |
| Undefined | A variable that has no value yet | let x; |
| Null | An intentional “empty” value | null |
Here they are in code:
const name = "Alex"; // Stringconst age = 25; // Numberconst isStudent = true; // Booleanlet address; // Undefined (no value assigned)const car = null; // Null (intentionally empty)Let’s walk through each line to see which type we created:
nameholds text inside quotes, so it is a string.ageholds a plain number with no quotes, so it is a number.isStudentholdstrue, so it is a boolean.addressis declared but never given a value, so it is undefined.caris set tonull, which means we deliberately marked it as empty.
null vs undefined
undefined means a variable has not been given a value yet. null is a value
you set on purpose to say “this is empty”. They look similar but are used in
different situations.
📦 Objects
When you need to group related values together, you use an object or an array. These are not primitive types.
// An object: a collection of named valuesconst user = { name: "Alex", age: 25,};
// An array: an ordered list of valuesconst colors = ["red", "green", "blue"];Here we group several values into two containers:
useris an object written with curly braces{}, where each value has a name, called a key. It storesnameset to"Alex"andageset to25.colorsis an array written with square brackets[], where values sit in order. It holds three strings:"red","green", and"blue".
We will cover objects and arrays in detail in later lessons.
🔍 Checking the Type With typeof
You can check the type of any value using the typeof operator. It returns the type as a string.
console.log(typeof "Hello"); // "string"console.log(typeof 42); // "number"console.log(typeof true); // "boolean"console.log(typeof undefined); // "undefined"Each line asks typeof to report the type of a value, and console.log prints the answer:
typeof "Hello"returns"string"because the value is text.typeof 42returns"number"because the value is a number.typeof truereturns"boolean"because the value is true or false.typeof undefinedreturns"undefined"because the value has no assigned value.
⚠️ Common Mistakes to Avoid
| Mistake | Problem | Solution |
| Writing a number inside quotes | "42" is a string, not a number | Remove the quotes to make it a number: 42 |
Expecting typeof null to be “null” | It actually returns "object", a long-standing quirk | Remember this behavior when checking for null |
Confusing null and undefined | They mean different things | Use undefined for “not set” and null for “empty on purpose” |
🔧 Try It Yourself!
- Create one variable of each type: a string, a number, and a boolean.
- Print each variable’s type using
typeof. - Create a variable without giving it a value and print its type.
- Create an object with two properties and print one of them.
🧩 What You’ve Learned
- ✅ A data type describes the kind of value you are working with
- ✅ Primitive types include string, number, boolean, undefined, and null
- ✅ Objects and arrays group multiple values together
- ✅
undefinedmeans “no value yet”;nullmeans “empty on purpose” - ✅
typeoftells you the type of a value
🚀 What’s Next?
Now that you know the data types, we will learn how to convert values from one type to another. Let’s continue to Type Conversion.