Last updated

JavaScript Data Types

Definition: A data type is the kind of a value. It decides what you can do with that value.

The main types

  • number10 or 3.14 (one type for both)
  • string — text, e.g. "hello"
  • booleantrue or false
  • array — a list, e.g. [1, 2, 3]
  • object — key/value data, e.g. {name: "Sam"}
  • undefined — a variable with no value yet

Example 1 — checking a type

console.log(typeof 10);
console.log(typeof "hello");
console.log(typeof true);

Example 2 — one number type

let whole = 10;
let decimal = 3.14;
console.log(typeof whole, typeof decimal); // number number

Example 3 — type changes behaviour

console.log(5 + 5);       // 10 (numbers add)
console.log("5" + "5");   // "55" (strings join)

💡 Note: typeof an array says "object" (arrays are special objects). To detect an array specifically, use Array.isArray(x).

Try it Yourself
Output

          
Ad · responsive