JavaScript Data Types: A Complete Beginner Guide
JavaScript has 8 data types split into primitives and objects. Learn each type, how to check them with typeof, and which one to use for different kinds of data.
Every value in JavaScript belongs to one of 8 data types. These types determine what you can do with a value, how it behaves in expressions, and how it is stored in memory.
The 8 types split into two categories. Seven are primitives: simple, immutable values that represent a single piece of data. One is object: a mutable collection that can hold multiple values and even other objects.
Here is the full list at a glance:
| Type | Category | Example | What it stores |
|---|---|---|---|
| string | Primitive | "hello" | Text |
| number | Primitive | 42, 3.14 | Integers and floats |
| bigint | Primitive | 9007199254740991n | Very large integers |
| boolean | Primitive | true, false | Logical yes/no |
| undefined | Primitive | undefined | Not assigned yet |
| null | Primitive | null | Intentionally empty |
| symbol | Primitive | Symbol("id") | Unique identifiers |
| object | Non-primitive | { name: "Alex" } | Collections of data |
string: Text Values
A string stores a sequence of characters. You create strings with single quotes, double quotes, or backticks. The choice is mostly a style preference, though backticks enable template literals with embedded expressions.
let single = 'Hello';
let double = "World";
let template = `The result is ${10 + 5}`;
console.log(single, double, template);The console prints "Hello", "World", and "The result is 15". The backtick version evaluates the expression inside the curly braces and inserts the result into the string.
Strings are immutable. Methods like toUpperCase do not change the original string; they return a new one. You can check if a value is a string with the typeof operator, which returns the string "string".
number: Integers and Floats
JavaScript has a single number type that covers both integers and floating-point values. There is no separate int or float type like in other languages. All numbers are 64-bit floating-point values following the IEEE 754 standard.
let integer = 42;
let float = 3.14;
let negative = -10;
let scientific = 1.5e6; // 1,500,000
console.log(typeof integer, typeof float);Both typeof calls return "number". The number type also includes special values like Infinity, -Infinity, and NaN (Not a Number), which represents the result of an invalid math operation.
Numbers are precise up to about 15 digits. For integers larger than 2^53 - 1, precision starts to degrade. That is where bigint comes in.
bigint: Very Large Integers
bigint was introduced in ES2020 for integers too large for the number type. You create a bigint by appending n to an integer literal or by calling the BigInt function.
let safe = 9007199254740991; // max safe integer for number
let big = 9007199254740991n; // bigint
let huge = BigInt("9007199254740991999");
console.log(typeof big);The typeof check returns "bigint". You can perform arithmetic with bigints, but you cannot mix them with regular numbers in the same operation without explicit conversion. Bigints are useful for cryptography, high-precision timestamps, and large database IDs.
boolean: True or False
A boolean has exactly two possible values: true or false. Booleans are the result of comparisons and logical operations. They control the flow of your program through if statements and loops.
let isLoggedIn = true;
let isExpired = false;
console.log(10 > 5); // true
console.log("a" === "b"); // falseEvery value in JavaScript can be converted to a boolean context. Values that convert to false are called falsy: false, 0, "" (empty string), null, undefined, and NaN. Everything else is truthy.
This is the foundation of conditional logic in JavaScript.
undefined: No Value Assigned
undefined is the default value of a variable that has been declared but not assigned. It is also the return value of functions that do not explicitly return anything.
let unassigned;
console.log(unassigned); // undefined
console.log(typeof unassigned); // "undefined"
function noReturn() {}
console.log(noReturn()); // undefinedundefined means the absence of a value was not intentional. The JavaScript engine put it there because nothing else was assigned. You should rarely assign undefined to a variable yourself; use null for intentional emptiness instead.
null: Intentionally Empty
null is an explicit assignment that means this value is empty on purpose. It represents the intentional absence of any object value.
let selectedUser = null; // no user selected yet
console.log(selectedUser); // null
console.log(typeof selectedUser); // "object" (historical bug)The typeof null returning "object" is a well-known bug in JavaScript that dates back to its first version. It has never been fixed because too much existing code depends on it. To check for null reliably, use a strict equality comparison: value === null.
For a deeper comparison of undefined and null, see the undefined vs null guide.
symbol: Unique Identifiers
A symbol is a guaranteed unique value, even if two symbols are created with the same description. Symbols were introduced in ES6 and are mainly used as unique property keys on objects.
let id1 = Symbol("id");
let id2 = Symbol("id");
console.log(id1 === id2); // falseEven though both symbols have the same description "id", they are completely different values. Symbols are useful when you need to add a property to an object without risking a name collision with other code. They are an advanced feature that most beginners do not need immediately.
object: Collections of Data
An object is a mutable collection of key-value pairs. Almost everything that is not a primitive is an object. This includes plain objects, arrays, functions, dates, regular expressions, maps, and sets.
let user = { name: "Alex", age: 30 };
let colors = ["red", "green", "blue"];
let greet = function() { return "Hello"; };
console.log(typeof user); // "object"
console.log(typeof colors); // "object"
console.log(typeof greet); // "function" (functions are callable objects)Objects are stored and copied by reference, not by value. When you assign an object to a new variable, both variables point to the same object in memory. Changing one affects the other.
This is the key behavioral difference between primitives and objects.
For a complete exploration of how primitives and objects behave differently in memory, see the primitive vs reference types guide.
Rune AI
Key Insights
- JavaScript has 8 data types: 7 primitives and 1 object type.
- Primitives include string, number, bigint, boolean, undefined, null, and symbol.
- Objects include arrays, functions, dates, and plain objects.
- Use typeof to check the type of any value.
- undefined is automatic; null is intentional.
Frequently Asked Questions
How many data types does JavaScript have?
What does typeof return for an array?
What is the difference between undefined and null?
Conclusion
JavaScript has 8 data types organized into primitives and objects. Primitives are immutable single values like strings, numbers, and booleans. Objects are mutable collections like arrays and plain objects. Understanding the type system helps you choose the right data structure and avoid type-related bugs.
More in this topic
JavaScript While Loop Explained: A Complete Guide
A while loop repeats code as long as a condition stays true. Learn the syntax, see practical examples, and understand when a while loop is the right choice over a for loop.
JavaScript Loops Tutorial: for, while, do while
Loops let JavaScript repeat code without writing it twice. Learn the three core loop types, what each one does, and when to choose one over another.
How to Loop Through Arrays Using JS for Loops Guide
Looping through arrays is the most practical use of JavaScript for loops. Learn how to access each element by index, avoid off-by-one errors, and choose between for, for...of, and forEach.