Stop 04 · JavaScriptTopics · 2/36

Datatypes

Part of the Frontend Developer Roadmap

In short

JavaScript decides the type of every value for you while the code runs. Knowing the types stops surprises like "5" + 1 giving "51" instead of 6.

JavaScript's primitive types (string, number, boolean, null, undefined, symbol, bigint) plus the object type — knowing them cold prevents subtle bugs. Type coercion, equality rules, numeric edge cases, and object references are particularly important because JavaScript is dynamically typed.

The jaadu mascot weighs tomatoes at a sabzi stall next to lemons and coriander.

Think of it like this

The sabzi stall

Tomatoes are sold by the kilo, lemons by the piece, and coriander comes free with a smile. Adding 2 kg to 5 lemons makes no sense. JavaScript values are similar: numbers, text and true or false each behave in their own way.

Watch it happen

typeof, one value at a time

'chai'
0
42
1
10n
2
true
3
undefined
4
null
5
[1, 2]
6

1/6typeof 'chai' gives "string". Text is always a string.

The big ideas

What you need to know

Seven primitives

string, number, bigint, boolean, undefined, null and symbol. They're simple values that can't be changed, only replaced.

Everything else is an object

Arrays, functions, dates and plain { } objects are all objects. They hold collections of values and are passed around by reference.

typeof tells you the type

typeof "hi" gives "string". It has quirks, so use Array.isArray to check for arrays.

Types are dynamic

The same variable can hold a number now and a string later. Flexible, but it's also where many bugs come from.

See it in code

Walk through the code

js
1console.log(typeof "chai");        // "string"2console.log(typeof 42);            // "number"3console.log(typeof 10n);           // "bigint"4console.log(typeof true);          // "boolean"5console.log(typeof undefined);     // "undefined"6console.log(typeof Symbol("id"));  // "symbol"7console.log(typeof null);          // "object" (an old bug, kept forever)8console.log(typeof [1, 2]);        // "object"9console.log(Array.isArray([1, 2])); // true

1/4Strings, numbers, bigints and booleans each report their own type name.

typeof null returning "object" is a mistake from JavaScript's first version. It can't be fixed without breaking old websites, so check for null with value === null.

Save yourself the pain

Common mistakes

Oops

Checking if (typeof value === "object") and assuming it's a real object.

Do this instead

null also passes that check. Write value !== null && typeof value === "object".

Oops

Expecting 0.1 + 0.2 === 0.3 to be true.

Do this instead

It gives 0.30000000000000004. For money, store amounts in paise as whole numbers and divide by 100 only for display.

Your mission

Type detective

~10 min

Quick check

What does typeof null return?

Go deeper

  1. 1Use `typeof` on each primitive type in the console to see the results. Verify the behavior with realistic data and handle the failure or empty state as well.
  2. 2Learn the difference between `null` and `undefined` precisely. Focus on understanding the underlying reason it works, not only memorizing the definition.
  3. 3Understand that everything non-primitive (arrays, functions, objects) is an object.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge