Data Types in JavaScript
When you're learning JavaScript, one of the most important things to understand early on is data types. Every piece of data in your program—text, numbers, true/false values, or even empty values—has a specific type. Knowing how data is categorized helps you write better code and avoid bugs.
Primitive vs Reference Types
In JavaScript, data types fall into two groups: primitive types and reference types.
Primitive types are the basic building blocks. These are simple, single values like a name, a number, or a true/false choice. They are:
string
number
boolean
undefined
null
symbol
bigint
Each time you use a primitive value, it’s stored directly and separately in memory.
Reference types, on the other hand, are more complex. The main one is the object
type. Objects group data together in a structured way, making it easier to manage related values.
Let’s break down each of the main data types you'll use all the time:
String
A string
is text. You wrap it in quotes—single, double, or backticks.
let name = "Sara";
console.log(typeof name); // Output: "string"
Strings
are used for things like names, messages, and anything written in words.
Number
A number
is any whole or decimal value. It doesn’t need quotes.
let age = 25;
console.log(typeof age); // Output: "number"
let temperature = -3.5;
console.log(typeof temperature); // Output: "number"
Note If you put a number in quotes, JavaScript treats it as a string.
Boolean
Booleans
are simple: they’re either true
or false
. They're great for making decisions.
let isOnline = true;
console.log(typeof isOnline); // Output: "boolean"
Undefined
This means a variable was created but hasn’t been given a value yet.
let city;
console.log(typeof city); // Output: "undefined"
Null
null
is used when you intentionally want a variable to have no value.
let score = null;
console.log(typeof score); // Output: "object" (this is a known quirk)
Even though typeof null
returns "object"
, it’s still considered a primitive value.
Symbol
Symbols
are unique and can be used to create hidden or private object keys.
let id = Symbol("id");
console.log(typeof id); // Output: "symbol"
They’re mostly used in advanced programming but good to know they exist.
BigInt
Use this when you need really large numbers—bigger than JavaScript usually handles.
let big = 123456789123456789123456789n;
console.log(typeof big); // Output: "bigint"
Checking Types with typeof
You can find out what type any value is by using the typeof
operator. It tells you whether something is a string, number, object, and so on.
console.log(typeof "Hello"); // Output: "string"
console.log(typeof 42); // Output: "number"
console.log(typeof true); // Output: "boolean"
console.log(typeof {}); // Output: "object"
You can use it with or without parentheses. Both work the same:
console.log(typeof(42)); // Output: "number"
console.log(typeof 42); // Output: "number"
This tool is super helpful when you're debugging or just trying to understand your code better.