JavaScript Data Types

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:

  1. Primitive types 🧱 - simple, single values like text, numbers, and true/false
  2. 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:

data-types.js
const name = "Alex"; // String
const age = 25; // Number
const isStudent = true; // Boolean
let address; // Undefined (no value assigned)
const car = null; // Null (intentionally empty)

Let’s walk through each line to see which type we created:

  • name holds text inside quotes, so it is a string.
  • age holds a plain number with no quotes, so it is a number.
  • isStudent holds true, so it is a boolean.
  • address is declared but never given a value, so it is undefined.
  • car is set to null, 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.

data-types.js
// An object: a collection of named values
const user = {
name: "Alex",
age: 25,
};
// An array: an ordered list of values
const colors = ["red", "green", "blue"];

Here we group several values into two containers:

  • user is an object written with curly braces {}, where each value has a name, called a key. It stores name set to "Alex" and age set to 25.
  • colors is 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.

data-types.js
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 42 returns "number" because the value is a number.
  • typeof true returns "boolean" because the value is true or false.
  • typeof undefined returns "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!

  1. Create one variable of each type: a string, a number, and a boolean.
  2. Print each variable’s type using typeof.
  3. Create a variable without giving it a value and print its type.
  4. 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
  • ✅ undefined means “no value yet”; null means “empty on purpose”
  • ✅ typeof tells 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.