Data Types

The different kinds of values JavaScript can store and work with.

What is it?

The number 5, the word "hello" and a true/false answer all behave differently. JavaScript groups values into a small set of data types so it knows how to handle each one.

The ones you'll use most:

  • String — text, written in quotes: "hello"
  • Number — any number: 42, 3.14
  • Boolean — true or false: true, false
  • Undefined — a variable that has been declared but has no value yet
  • Null — a value that's intentionally empty
  • Object — a collection of related data (including arrays and functions)

Explain like I'm 10

Think of a toolbox with compartments for screws, nails and tape. Data types are those compartments: they tell you what you're holding, so you know what you can do with it.

Examples

Checking a value's type

console.log(typeof "hello");   // "string"
console.log(typeof 42);        // "number"
console.log(typeof true);      // "boolean"
console.log(typeof undefined); // "undefined"

typeof is a built-in operator that tells you the data type of any value.

The same operator, different types, different behavior

console.log(2 + 3);       // 5   — both numbers, so + adds them
console.log("2" + "3");   // "23" — both strings, so + joins them
console.log("2" + 3);     // "23" — mixed, so the number becomes a string first

+ behaves differently depending on the types of its two values. That's why knowing a value's type matters.

How it works

JavaScript tags every value with a type. The type decides what operations do: you can do math on numbers, but adding two strings joins them ("2" + "3" becomes "23", not 5). Know the type, and you know how a value behaves.

Why does it exist?

Different data needs different rules. Text is joined and searched; numbers are added and compared; true/false values drive decisions. Types let the language apply the right rules automatically.

When to use it

Think about types when you're unsure what kind of value you have: checking user input, debugging an odd result, or deciding whether two values can be safely compared or combined. typeof is the quick way to check.

When not to use it

Don't check the type of values you created yourself and already know. typeof checks everywhere just add noise. Save them for boundaries: user input, API responses and arguments from code you don't control.

Common mistakes

  • Mixing up a number and a string that looks like a number, e.g. "5" + 1 gives "51", not 6.

  • Confusing undefined (nothing assigned yet) with null (intentionally empty).

  • Forgetting that arrays and functions are technically objects too.

Practice exercises

  1. Easy:

    Use typeof to check the type of a string, a number, and a boolean.

  2. Medium:

    Predict the output of "3" + 3 and 3 + 3, then check by running it.

  3. Hard:

    Write a function describeType(value) that returns a friendly sentence describing the value's type.

Interview questions

What are the basic data types built into JavaScript?

Seven primitive types (String, Number, Boolean, Undefined, Null, Symbol and BigInt) plus Object, which covers everything else: plain objects, arrays, functions, dates and more.

What's the difference between a primitive and an object?

A primitive holds its value directly and is compared and copied by value: two strings with the same characters are equal. An object is held by reference, so it's compared and copied by reference: two separately created objects with identical contents are not === equal.

What does `typeof` return for a string, a number, and a boolean?

"string", "number" and "boolean". typeof returns a string naming the value's type, handy for quick runtime checks.

What's the difference between `null` and `undefined`?

JavaScript gives you undefined automatically for a declared-but-unassigned variable, a missing argument or a missing property: nothing has been set yet. null is assigned deliberately by a developer to mean "intentionally empty".

What is `NaN`, and what does the name stand for?

"Not a Number": the value numeric operations return when the result isn't a valid number, like 0 / 0 or Number("abc"). Despite the name, typeof NaN is "number", since it belongs to the Number type.

Is an array its own separate data type in JavaScript?

No. Arrays are a special kind of object (with numeric-like indices and a length property). typeof [] returns "object"; to check for an array, use Array.isArray().

Is a function a data type in JavaScript?

Functions are callable objects. typeof returns "function" for them instead of "object", the one case where it tells a callable object apart from a plain one.

What does `typeof null` return, and why is that surprising?

It returns "object", though null is a primitive. It's a bug from the original 1995 implementation (null shared the internal type tag used for objects), kept only for backward compatibility.

What does `typeof` return for an array, and how would you actually check for one?

typeof [] returns "object", like any plain object, so typeof can't spot arrays. Use Array.isArray(value) instead.

What does `typeof` return for a function?

"function". It's the one case where typeof is more specific than "object" for something that is an object underneath.

What is type coercion?

JavaScript automatically converting a value to another type when an operation expects a different type. For example, turning a number into a string so + can join it to a string.

Why does `"2" + 3` produce `"23"` while `"2" - 1` produces `1`?

+ has a special rule: if either operand is a string, it turns the other into a string and joins them. Other arithmetic operators, including -, can't join strings, so they convert both operands to numbers: "2" becomes 2, and 2 - 1 is 1.

What's the difference between implicit and explicit type conversion?

Implicit conversion (coercion) happens automatically during an operation, like "5" * 2 silently becoming 10. Explicit conversion is done on purpose in code, like Number("5") or String(5), so the intent is visible.

How would you explicitly convert a string to a number?

Number("42"), parseInt("42") / parseFloat("42.5"), or unary +"42". Number() and unary + need the whole string to be numeric or they return NaN. parseInt/parseFloat read as much of the leading number as they can and ignore the rest.

How would you explicitly convert a number to a string?

String(42), (42).toString(), or 42 + "" (which relies on coercion, so it's less explicit). All produce "42".

What is `BigInt`, and why does it exist?

A primitive for whole numbers larger than Number can safely hold (Number.MAX_SAFE_INTEGER, 2^53 - 1), without losing precision. Add n to an integer literal to create one, e.g. 123n. You can't mix BigInt and regular numbers in arithmetic without converting explicitly.

What is `Symbol`, and what is it typically used for?

A primitive that is unique every time you create one (Symbol("id") !== Symbol("id")). It's often used as an object property key that can't collide with any string key or other symbol, which is useful for adding metadata to objects safely.

Why is `NaN === NaN` false?

The IEEE 754 floating-point spec (which JavaScript numbers follow) defines NaN as never equal to anything, including itself. It means "not a valid numeric result", not a specific value.

How do you correctly check whether a value is `NaN`?

Number.isNaN(value), which returns true only for NaN itself. The older global isNaN(value) converts its argument to a number first, so isNaN("hello") is also true.

What's the difference between `Number.isNaN()` and the global `isNaN()`?

Number.isNaN() is strict: true only if the value is exactly NaN, with no conversion. The global isNaN() converts its argument to a number first, so isNaN("abc") is also true, though "abc" was never NaN.

Is JavaScript strongly or weakly typed, and what does that mean in practice?

Weakly typed. Operators convert between types instead of throwing when types don't match: "5" * 2 silently becomes 10. That's forgiving, but a common source of subtle bugs when a conversion happens where you didn't expect it.

Is JavaScript statically or dynamically typed?

Dynamically typed. Types aren't fixed or checked at compile time; they belong to the current value and are only checked as the code runs. TypeScript's static types are added on top to address this.

Can a variable's type change after it's declared?

Yes. A variable is just a name for whatever value it currently holds, so let x = 5; x = "now text"; is legal. Only the value has a type, not the variable.

Why are primitives compared by value while objects are compared by reference?

Primitives are stored directly, so comparing them compares their contents: "cat" === "cat" is true. Objects are stored as references, so === checks whether both variables point to the same object. Two separate objects with identical properties are still !==.

What happens when you access a property on a primitive, like `"hello".length`?

JavaScript briefly wraps the primitive in its object type (here a String object), reads the property, then throws the wrapper away. The primitive never becomes an object; this "auto-boxing" just lets primitives use properties and methods.

Output-prediction: what does `typeof typeof 1` evaluate to?

"string". The inner typeof 1 returns the string "number", and the type of any string is "string".

Output-prediction: what does `console.log(1 + "1"); console.log(1 - "1");` log?

It logs "11" then 0. + sees a string, so it turns the number into a string and joins them. - can't join strings, so it turns "1" into 1, giving 1 - 1 = 0.

Output-prediction: what does `console.log([1, 2, 3] + [4, 5, 6])` log?

The string "1,2,34,5,6". + converts both arrays to strings first (arrays join their elements with commas), giving "1,2,3" and "4,5,6", which are then joined.

Output-prediction: what does `null == undefined` evaluate to? What about `null === undefined`?

null == undefined is true: the spec makes loose equality treat them as equal to each other (and to nothing else). null === undefined is false, because strict equality also needs the same type.

Output-prediction: what does `typeof NaN` return?

"number". Despite its name, NaN belongs to the Number type: it's an invalid numeric result, not a missing number.