SKU // 95

JavaScript Data Types

Back in JS Types you met the basic kinds of values. Now that you have used strings, numbers, arrays, objects, Maps, and more, this section goes deeper: exactly how many types there are, the one big split that divides them, and the tools for telling them apart reliably.

// There Are Exactly Eight

Type Example Kind
string"hello"primitive
number42, 3.14, NaNprimitive
bigint9007199254740993nprimitive
booleantrue, falseprimitive
undefinedundefinedprimitive
nullnullprimitive
symbolSymbol("id")primitive
object{ }, [ ], functions, dates, maps...object

Seven primitives, plus object. Every value in JavaScript is one of these eight. Arrays, functions, dates, Maps, Sets, and regexes are all kinds of object, not separate types.

// One of Each

Here is a declaration for every type, checked with typeof (every detail is in JS typeof):

const length = 16;                            // number
const weight = 7.5;                           // number (there is no separate decimal type)
const big = 1234567890123456789012345n;       // bigint
const color = "Yellow";                       // string
const done = false;                            // boolean
let nothingYet;                               // undefined
const empty = null;                            // null
const id = Symbol();                          // symbol
const person = { firstName: "John", lastName: "Doe" };   // object
const cars = ["Saab", "Volvo", "BMW"];         // object (an array)
const date = new Date("2022-03-25");          // object (a Date)

console.log(typeof length, typeof big, typeof color, typeof done);
console.log(typeof nothingYet, typeof empty, typeof id, typeof person, typeof cars, typeof date);
number bigint string boolean undefined object symbol object object object

Two surprises in the second line: typeof null says "object" (a famous bug, see JS typeof), and arrays and dates report "object" because they are objects.

W3Schools errors on its Data Types page
  • Its BigInt example BigInt(1234567890123456789012345) passes an ordinary number, which has already lost precision before BigInt sees it. The result is 1234567890123456824475648n. Use the n suffix or a string: BigInt("1234567890123456789012345").
  • Its examples declare let x and let y over and over. Run as one program, the second let x is SyntaxError: Identifier 'x' has already been declared. Each pair is meant as a separate example.

// The Big Split: Primitives vs Objects

The single most important idea in this section. Primitives and objects behave differently when you copy, compare, and change them:

// Primitives: copied by VALUE
let a = 5;
let b = a;       // b gets its own 5
b = 10;
console.log(a);  // 5  (a is unaffected)

// Objects: copied by REFERENCE
const x = { n: 5 };
const y = x;     // y points to the SAME object
y.n = 10;
console.log(x.n); // 10 (x sees the change)
5 10
Primitives Objects
Assign / pass to a functionCopies the valueShares the same object
Compare with ===Same value?Same object?
Can be changed in placeNeverYes
"hi" === "hi"            // true   same value
[1, 2] === [1, 2]        // false  two different arrays
true false
Picture It
A primitive is like writing a number on a sticky note: copying it means writing a second note. An object is like a house: copying the variable just copies the address, and both addresses lead to the same house.

// Why Types Matter

The computer has to know a value's type to know what an operator should do. What is 16 + "Volvo"? JavaScript's rule: if either side of + is a string, the other side becomes a string and the two are joined:

console.log(16 + "Volvo");
console.log("Volvo" + 16);
console.log(16 + 4 + "Volvo");     // left to right: 16 + 4 happens first
console.log("Volvo" + 16 + 4);     // a string first, so everything is joined
16Volvo Volvo16 20Volvo Volvo164

+ works left to right. In the third line, 16 + 4 is plain math (20) before the string shows up. In the fourth, the string comes first, so 16 and then 4 are each joined as text. These automatic conversions are called type coercion, and they get their own lesson in Part 2 of this section (JS Type Coercion).

// Dynamic Typing, Revisited

Variables do not have types; values do. The same variable can hold a string now and a number later:

let data = "42";      // string
data = Number(data);  // number
data = [data];        // object (an array)

That flexibility is why the tools in this section matter: at any moment, you may need to check what kind of value you are actually holding.

// Telling Types Apart

typeof "hi"              // "string"   works well for most primitives
typeof null              // "object"   famous mistake: check with === null
typeof [1, 2]            // "object"   use Array.isArray
Array.isArray([1, 2])     // true
new Date() instanceof Date // true       which constructor made it
string object object true true

// The Road Ahead

Primitive DataWhy primitives can never change, and how "hi".length works anyway
Object TypesReferences, shallow vs deep copies, identifying objects
SymbolsThe least familiar primitive
typeofEvery result, every quirk
undefinedWhere it comes from and how to handle it

// Quick Check

What does this log?

console.log(1 + 2 + "3" + 4 + 5);
Show answer
3345. Left to right: 1 + 2 is 3, then 3 + "3" is the string "33", and from then on every + joins text: "334", then "3345".

Which of these is not one of JavaScript's eight data types?

  1. bigint
  2. symbol
  3. array
  4. null
Show answer
C. Arrays are objects. The eight types are string, number, bigint, boolean, undefined, null, symbol, and object.

With let a = 5; let b = a; b = 10;, a stays 5. With const a = {n: 5}; const b = a; b.n = 10;, a.n becomes 10. Why the difference?

Show answer
Primitives are copied by value, so b gets its own 5. Objects are copied by reference, so b points to the same object as a, and a change through either name is seen by both.

// Try It

  1. Click Open Playground and run it: one example of each of the eight types. Which result looks wrong, and why?
  2. Copy a number into a second variable and change it; then do the same with an object's property
  3. Compare two identical arrays with ===, then two identical strings
  4. Write a function that receives an array and pushes to it, and show the original array changed
// Video Walkthroughs
Last updated: Oct 10, 2026, 1:47 AM ET