cf.completefrontendCode editorOpen lab
THE JAVASCRIPT FIELD GUIDE

Data types

Meet JavaScript's seven primitive types and objects, learn what typeof really reports, and see why variables can hold different kinds of values over time.

By the end, you can
  • 01
    Separate primitives from objectsPredict when copying makes an independent value and when it shares an object reference.
  • 02
    Name all eight JavaScript typesRecognize strings, numbers, booleans, BigInts, symbols, null, undefined, and objects.
  • 03
    Use typeof honestlyRead real typeof results, including null, arrays, functions, and NaN.

Values have types

Every value in JavaScript has a type: a small label that tells the language what kind of value it is and what operations make sense. The variable name is not the type. The value inside the variable is.

You already met variables in Variables: let, const & var. That lesson separated reassignment (point a variable at a new value) from mutation (change an object through an existing reference). Data types explain why that distinction matters.

The short definition

JavaScript has eight type categories: seven primitive types plus object. Primitive values are small, immutable values; objects are collections that variables refer to by reference.

If you saw Why JavaScript?, you saw "5" + 3 become "53". That happened because one value was a string and the other was a number. This lesson names those categories without going deep on any one: each type gets its own lesson soon, starting with Numbers & math next.

Primitives vs objects

STEP THROUGH

JavaScript values first split into two families: primitives and objects. A primitive is a value itself: 5, "Ada", true, null. You do not open it and change a part of it. If you want a different primitive, you replace it with another primitive.

Objects are different. An object can hold properties, and code can change those properties. Variables do not contain the whole object; they contain a reference to it. Copy the reference, and two variables can reach the same object.

Real-life analogyPrimitives are sticky notes

Imagine a as a sticky note with 5 written on it. When b = a, you get another sticky note with 5. Changing b to 6 does not rewrite the first note.

In real life: A sticky note that says 5
In JavaScript: A primitive number value
In real life: Photocopy the note
In JavaScript: Copying a primitive to another variable
In real life: Write 6 on the second note
In JavaScript: Reassign the second variable
In real life: The first note still says 5
In JavaScript: The first variable is unchanged

Where the analogy stops: Real sticky notes can be scribbled on. Primitive values cannot be changed in place; the variable is given a replacement value.

Real-life analogyObjects are houses; variables hold addresses

An object is more like a house. The variable holds the address. Copying the address does not build a second house. If someone changes the house, everyone with that address sees the change.

In real life: A house
In JavaScript: An object like { n: 1 }
In real life: An address card
In JavaScript: A variable that refers to the object
In real life: Copy the address card
In JavaScript: Assign one object variable to another
In real life: Paint the house door
In JavaScript: Mutate a property; every copied address sees it

Where the analogy stops: JavaScript references are not literal street addresses and you cannot do address arithmetic. The useful idea is sharing: two references can reach one object.

Copy a primitive, then copy an object reference
Step 0 of 8Ready
Your turn: follow the blue line

Predict both printed values. Then step through the primitive copy and the object-reference copy.

Running in
  1. script
Next: line 1
Click the blue line to take the next stepPop out in the code editor (opens in a new tab)JavaScript
let b = a;b = 6;console.log(a); const box1 = { n: 1 };const box2 = box1;box2.n = 2;console.log(box1.n);
CallStoreChangeResultRun = next line. Ran = already executed.
Recent returnsNothing yet. Start with the blue line.
Change line 8: what value should the shared object get?

Changing the setting starts a fresh recorded run.

A guided replay recorded from real JavaScript calls, not an engine debugger. Step follows executed statements; Back reviews a snapshot. Reset starts a fresh run.

The first half prints 5 because numbers are primitives copied by value. The second half prints 2 because both names point at one object. Later lessons, Objects & references and Memory: primitives vs objects, go deeper into references. For now, hold onto the prediction rule: primitives copy the value; object variables copy the reference.

The eight types

TABLE

Here is the whole pantry. Seven types are primitive: string, number, bigint, boolean, undefined, symbol, and null. The eighth type is object, which covers plain objects, arrays, dates, functions, Math, and most structured things you work with.

Real-life analogyEight pantry shelves

Think of the eight types as pantry shelves. You do not memorize every can on day one. You learn the shelf names so you know where to look next.

In real life: A shelf for flour, a shelf for sugar
In JavaScript: A type category for values
In real life: A recipe asks for an ingredient kind
In JavaScript: An operation expects a kind of value
In real life: One pantry contains many brands
In JavaScript: One type contains many possible values

Where the analogy stops: Real ingredients can sometimes be swapped by taste. JavaScript has precise rules; if a value is the wrong type, it may convert it or produce a surprising result.

JavaScript's eight type categories
TypeExample literaltypeof resultNotes
string"hi"stringText. Covered next in the Strings lesson.
number42numberIntegers, decimals, NaN, and Infinity. Next: Numbers & math.
bigint10nbigintWhole numbers beyond safe number range. See the BigInt lesson.
booleantruebooleanTrue or false values. See Booleans, null & undefined.
undefinedundefinedundefinedUsually means no value has been assigned yet.
symbolSymbol("id")symbolUnique primitive identifiers.
nullnullobjectAn intentional empty value; typeof says object because of history.
object{}objectObjects, arrays, functions, dates, Math, and more.

A few notes keep this table honest. null is listed with primitives because it is a primitive value, even though typeof null reports "object". Functions are objects, even though typeof reports "function". Arrays are objects too; JavaScript gives you Array.isArray for that specific check.

Primitive or object?
  • 42
  • "text"
  • true
  • null
  • undefined
  • 10n
  • Symbol()
  • [1, 2]
  • { a: 1 }
  • function () {}
  • new Date()
  • Math
Try it yourself
0 of 12 correct

Sort each value into the primitive family or the object family. Read the explanation for every edge case.

Choose a category for every card. You can change an answer at any time; Reset clears them all.

The typeof operator

INTERACTIVE

typeof is an operator: a built-in piece of JavaScript syntax that asks “what type name should I report for this value?” It returns a string such as "number" or "object".

typeof explorer
const value = null;console.log(typeof value);console.log(Array.isArray(value));
Real resultnull
Displayed valuenull
typeof valueobject
Array.isArray(value)false
Try it yourself

null gives typeof "object". typeof null is object, a historical bug kept so old websites do not break.

The result is computed with JavaScript's real typeof operator in this browser. The source shows the shape; the selected value changes line 1.

Use typeof for broad checks: string, number, bigint, boolean, undefined, symbol, function, or object. Then patch the known rough edges:

  • typeof null is "object", a historical bug kept for compatibility. Check value === null first when null matters.
  • typeof [] is "object". Use Array.isArray(value).
  • typeof function () is "function". Functions are callable objects; this special result is useful.
  • typeof NaN is "number". NaN is a special number value.
The array-check bugPop out in the code editor (opens in a new tab)JavaScript
function describe(value) {
  if (typeof value === "array") {
    return "array";
  }
  return typeof value;
}

console.log(describe([1, 2]));

There is no "array" result from typeof, so this program prints "object". The fix is a specific array check:

The array-check fixPop out in the code editor (opens in a new tab)JavaScript
function describe(value) {
  if (Array.isArray(value)) {
    return "array";
  }
  return typeof value;
}

console.log(describe([1, 2]));

Dynamic typing

STEP THROUGH

JavaScript is dynamically typed. That means variables do not have fixed type declarations. A variable made with let can hold a number now, a string later, and a boolean after that. The type travels with the current value.

Real-life analogyThe label is on the thing inside the box

Picture a reusable box named value. The box name stays the same, but the label belongs to what is inside it. Put in 42 and the label says number. Swap in "forty-two" and the label says string.

In real life: A box named value
In JavaScript: The variable name
In real life: A label on the item inside
In JavaScript: The value's type
In real life: Swap a toy for a book
In JavaScript: Reassign a different type of value

Where the analogy stops: A physical box can hold many things at once. A JavaScript variable holds one value at a time; reassignment replaces that value.

A variable can hold different types over time
Step 0 of 6Ready
Your turn: follow the blue line

Watch one variable hold a number, then a string, then a boolean. Each typeof result follows the current value.

Running in
  1. script
Next: line 1
Click the blue line to take the next stepPop out in the code editor (opens in a new tab)JavaScript
console.log(typeof value);value = "forty-two";console.log(typeof value);value = true;console.log(typeof value);
CallStoreChangeResultRun = next line. Ran = already executed.
Recent returnsNothing yet. Start with the blue line.
A guided replay recorded from real JavaScript calls, not an engine debugger. Step follows executed statements; Back reviews a snapshot. Reset starts a fresh run.

Dynamic typing is convenient for learning and quick scripts, but it also means you must be clear about what kind of value your code expects. TypeScript, a later lesson, adds static type checking on top of JavaScript so tools can warn you before the program runs.

Where you’ll use this

Type checks appear when code receives a value from outside: a form input, a configuration object, a saved setting, or an API response. Early lessons keep examples tiny, but professional code uses the same moves.

A safe value describerPop out in the code editor (opens in a new tab)JavaScript
function describeValue(value) {
  if (Array.isArray(value)) return "array";
  if (value === null) return "null";
  return typeof value;
}

console.log(describeValue(["Ada", "Lin"]));
console.log(describeValue(null));

This function handles the two typeof traps first: arrays and null. Only then does it return the broad typeof result. The deeper lesson Checking types reliably will build this into a stronger checklist.

Common misconceptions

“typeof tells me every exact kind.”

It reports broad type names. Arrays, dates, and plain objects all report "object"; use more specific checks when needed.

“null is an object.”

null is a primitive value. Only typeof null says "object", and that is historical compatibility.

“const makes objects immutable.”

const prevents reassignment of the variable. It does not freeze the object; const box = { n: 1 } can still have box.n changed.

“A copied object is a new object.”

Assignment copies the reference. Two variables can point at one object until you create a real copy.

“NaN must have its own type.”

NaN is a number value. It means a numeric calculation failed, not that JavaScript made a ninth type.

Practice: spot the type

5 EXERCISES
Exercise 1 · Warm-upPredict typeof outputs

Predict the four printed lines.

Starter codePop out in the code editor (opens in a new tab)JavaScript
console.log(typeof 42);
console.log(typeof null);
console.log(typeof [1, 2]);
console.log(typeof NaN);

Answer, then press Check. Spacing and letter case don’t matter.

    Exercise 2 · PracticeFix the array check

    The function tries to detect arrays with typeof. What expression should replace the broken condition?

    Starter codePop out in the code editor (opens in a new tab)JavaScript
    function describe(value) {
      if (typeof value === "array") {
        return "array";
      }
      return typeof value;
    }
    
    console.log(describe([1, 2]));

    Answer, then press Check. Spacing and letter case don’t matter.

      Exercise 3 · PracticeCategorize a built-in

      Is Math a primitive or an object?

      Answer, then press Check. Spacing and letter case don’t matter.

        Exercise 4 · PracticeExplain the changed copy

        Predict the output, then explain in your own words why it happens.

        Starter codePop out in the code editor (opens in a new tab)JavaScript
        const box1 = { n: 1 };
        const box2 = box1;
        box2.n = 2;
        console.log(box1.n);

        Answer, then press Check. Spacing and letter case don’t matter.

          Exercise 5 · ChallengeCount the primitive types

          How many primitive types does JavaScript have?

          Starter codePop out in the code editor (opens in a new tab)JavaScript
          console.log(["string", "number", "bigint", "boolean", "undefined", "symbol", "null"].length);

          Answer, then press Check. Spacing and letter case don’t matter.

            Quiz: check your understanding

            7 QUESTIONS

            Every answer explains itself. For code questions, predict first, then choose.

            Lesson quiz · 7 questionsScore: first tries count
            1. Question 1 of 7Which statement best describes a primitive value?

              Choose an answer to see the explanation.

            2. Question 2 of 7After copying a primitive, what does this print?

              Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
              let a = 5;
              let b = a;
              b = 6;
              console.log(a);

              Choose an answer to see the explanation.

            3. Question 3 of 7Which list contains only primitive types?

              Choose an answer to see the explanation.

            4. Question 4 of 7What does typeof null print?

              Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
              console.log(typeof null);

              Choose an answer to see the explanation.

            5. Question 5 of 7How should you check whether a value is an array?

              Choose an answer to see the explanation.

            6. Question 6 of 7After reassignment to text, what does this print?

              Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
              let value = 42;
              value = "forty-two";
              console.log(typeof value);

              Choose an answer to see the explanation.

            7. Question 7 of 7What does typeof NaN print?

              Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
              console.log(typeof NaN);

              Choose an answer to see the explanation.

            Key takeaways

            • JavaScript has eight type categories: seven primitives plus object.
            • Primitives are immutable values; objects are mutable collections reached through references.
            • typeof is useful, but remember null, arrays, functions, and NaN.
            • Variables are dynamically typed: the current value has the type, not the variable name.
            • Use Array.isArray for arrays and value === null for null.

            One-liner.
            A JavaScript data type tells you what kind of value you have, and that determines how copying, checking, and operations behave.

            Up next: Numbers & math.

            CompleteFrontend Clear concepts. Working examples.