Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 156 ยท Data Types

Data Types

Learn the eight JavaScript data types, the difference between primitives and objects, how typeof works and where it surprises you, and what dynamic typing means.

Every value in JavaScript has a type. The number 42, the text "hello" and the list [1, 2, 3] are different kinds of values, and the type decides what you can do with each one. You can multiply numbers, change the case of text and add items to a list, but not the other way around.

This chapter looks at each type in detail. This first lesson gives you the big picture: the eight types the language has, the important difference between primitives and objects, how to ask a value for its type with typeof, and what it means that JavaScript is dynamically typed.

The eight types

JavaScript has exactly eight data types. Seven of them are primitive types, which hold a single, simple value:

Type Example values Used for
number 42, 3.14, -7, NaN Integers and decimals (Numbers)
bigint 9007199254740993n Whole numbers too large for number (BigInt and Symbol)
string "hello", 'A', `Hi ${name}` Text (Strings)
boolean true, false Yes/no answers (Booleans)
undefined undefined "No value has been given"
null null "Intentionally empty" (null and undefined)
symbol Symbol("id") Unique keys for object properties

The eighth type is object. Objects are collections of values, and almost everything that isn't a primitive is an object: plain objects like { name: "Ada" }, arrays, functions, dates, maps, sets and errors.

JavaScript
const user = { name: "Ada", born: 1815 };   // a plain object
const scores = [12, 19, 7];                 // an array (a kind of object)
function greet() {}                         // a function (also an object)
const today = new Date();                   // a date (an object)

You'll spend most of your time with numbers, strings, booleans, null, undefined, objects and arrays. bigint and symbol are specialist tools you'll meet at the end of this chapter.

Asking for the type with typeof

The typeof operator returns the name of a value's type as a string. You write it before the value, with no parentheses needed:

JS typeof for every type

Press Run (or Ctrl + Enter) to see the console output here.

No output.

typeof is useful for checking input before you use it, such as making sure a value is a number before doing math with it. It also has a special talent: typeof someName returns "undefined" for a name that was never declared, instead of throwing a ReferenceError. That's why it's often used to check whether a feature exists, as you saw in Where JavaScript Runs.

The quirks of typeof

typeof has three results that surprise everyone at first. Memorize them now and you'll save yourself some confusion later:

JS Three typeof surprises

Press Run (or Ctrl + Enter) to see the console output here.

No output.

  • typeof null is "object". This was a mistake in the very first version of JavaScript in 1995. Fixing it would break millions of websites, so it stays. To check for null, compare directly: value === null.
  • Arrays report "object". Use Array.isArray(value) to check for an array.
  • Functions report "function". They're objects too, but typeof gives them a special result because "can I call this?" is such a useful question.

Primitives versus objects

The seven primitives are string, number, bigint, boolean, undefined, null and symbol. Everything else is a non-primitive (reference) type, and in JavaScript that means an object. Non-primitive types include plain objects, arrays, functions, dates, regular expressions, Map, Set and every class instance. They can hold many values, have properties and methods, can be changed in place, and are passed around by reference.

The split between primitives and objects isn't just a naming detail. It changes how values behave in two important ways.

Primitives can't be changed

A primitive value is immutable: it can never be modified. Methods that seem to change a string actually create a new string and leave the original as it was:

JavaScript
const word = "hello";
const loud = word.toUpperCase();
console.log(word); // hello (unchanged)
console.log(loud); // HELLO (a new string)

You can reassign a let variable to a different primitive, but the old value itself never changes. Objects, on the other hand, can be changed in place: you can add, change and remove their properties.

Copied by value, shared by reference

When you assign a primitive to another variable, the value is copied. The two variables are completely independent. When you assign an object, only a reference (think of it as the object's address) is copied. Both variables then point to the same object:

JS Values versus references

Press Run (or Ctrl + Enter) to see the console output here.

No output.

This is the reason a const array can still change, as you saw in Variables: the variable holds a fixed reference, but the object behind it can be modified. Sharing objects is efficient and often exactly what you want, but it is also a common source of bugs. Copying Objects shows how to make real copies when you need them.

Primitives that act like objects

If primitives aren't objects, how can you write "hello".length or (3.14159).toFixed(2)? When you use a property or method on a primitive, JavaScript briefly wraps it in a matching object (String, Number or Boolean), uses the method, and throws the wrapper away. It all happens automatically, so you can treat strings and numbers as if they had methods. You should never create these wrappers yourself with new String("hi"); they behave like objects, not like normal strings, and cause confusing comparisons.

Dynamic typing

In some languages, such as Java or C#, you must declare the type of each variable, and it can only ever hold that type. JavaScript is dynamically typed: variables don't have types, values do. Any variable can hold any type, and can switch to a different type later:

JS One variable, many types

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Dynamic typing makes JavaScript quick to write and flexible. The price is that type mistakes show up only when the code runs, sometimes far from where the wrong value came from. A classic example is a number that arrives as a string from a form field, so + joins text instead of adding.

You can avoid most of those problems with a few habits:

  • Keep each variable to one type for its whole life, even though the language allows switching.
  • Convert input early: turn text into numbers as soon as you read it (Type Conversion and Coercion shows how).
  • Check types with typeof or Array.isArray() where values come from outside your code.
  • Use === for comparisons, which never converts types behind your back (Comparison and Equality).

What about TypeScript? Many teams write their code in TypeScript, a language that adds type annotations on top of JavaScript, such as let count: number = 0. A tool checks the types before the code runs and then strips them out, leaving plain JavaScript. Everything you learn in this course applies directly to TypeScript.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. How many data types does JavaScript have, and how many of them are primitives?

    The seven primitive types are number, bigint, string, boolean, undefined, null and symbol. The eighth type is object.
  2. 2. What does typeof null return?

    This is a long-standing quirk from the first version of JavaScript. Check for null with value === null.
  3. 3. How can you reliably check whether a value is an array?

    typeof returns "object" for arrays and for plain objects alike. Array.isArray() tells them apart.
  4. 4. After const a = { n: 1 }; const b = a; b.n = 2;, what is a.n?

    Assigning an object copies the reference, so a and b point to the same object. Changing it through b is visible through a.
  5. 5. What does dynamic typing mean in JavaScript?

    In JavaScript, values have types but variables don't, so a variable can hold a number now and a string later.

Summary

  • JavaScript has seven primitive types (number, bigint, string, boolean, undefined, null, symbol) and objects. Arrays, functions and dates are all objects.
  • typeof returns a value's type as a string. Watch for its quirks: typeof null is "object", arrays are "object" (use Array.isArray()), and functions are "function".
  • Primitives are immutable and copied by value. Objects can be changed and are shared by reference.
  • Two different objects are never ===, even with identical contents.
  • JavaScript is dynamically typed: values have types, variables don't. Keep each variable to one type and convert input early.

Next: Numbers โ€” integers, decimals, NaN, Infinity and number methods.