Skip to content
C Codeloom
JavaScript

JavaScript Data Types: The Complete Beginner Guide

Every JavaScript data type explained — strings, numbers, booleans, null, undefined, symbols, bigints, and objects — with practical examples and how to check types correctly.

·7 min read · By Yash Kesharwani
Beginner 10 min read

What you'll learn

  • The seven primitive types and the one non-primitive type
  • The difference between null and undefined
  • How to check the type of any value
  • Why typeof has one famous bug
  • How JavaScript converts between types automatically

Prerequisites

JavaScript has a small, learnable set of built-in types. Knowing them — and knowing how to tell them apart — eliminates a large class of bugs that catch every beginner.

This post walks through every type, the typeof operator, the famous null quirk, and JavaScript’s habit of converting types automatically (which is occasionally helpful and occasionally maddening).

The two families: primitives and objects

JavaScript values fall into two big families.

Primitives are simple, immutable values:

  • string
  • number
  • boolean
  • null
  • undefined
  • bigint
  • symbol

Objects are everything else — arrays, plain objects, functions, dates, regular expressions, classes you write yourself, and so on. Internally they are all the same kind of thing: a collection of key/value properties.

We will deal with each in turn.

String

A string is a sequence of text characters. Three quote styles work:

const a = "double quotes";
const b = 'single quotes';
const c = `backticks (template literal)`;

Backtick strings are special — they support multi-line text and ${...} interpolation:

const name = "Ada";
const greeting = `Hello, ${name}!
Welcome back.`;
console.log(greeting);

Modern style is: backticks when you need interpolation or newlines, otherwise single or double — pick one and stay consistent. We will cover strings in depth in Strings and Template Literals.

Number

JavaScript has a single number type that covers integers and decimals:

const age = 30;
const price = 19.99;
const negative = -5;
const tiny = 1e-3;       // 0.001
const huge = 2.5e9;      // 2,500,000,000

There are also three special numeric values that you will eventually run into:

console.log(1 / 0);      // Infinity
console.log(-1 / 0);     // -Infinity
console.log(0 / 0);      // NaN  ("not a number")

NaN is a number that represents an invalid mathematical result. Confusingly, typeof NaN is "number". We will say more about this in Numbers and Math.

Boolean

A boolean has exactly two values: true and false.

const isAdmin = true;
const hasShipped = false;
const isAdult = age >= 18;

Booleans show up everywhere — in if conditions, while loops, and as the result of comparison operators like ===, <, and >.

null and undefined

These are JavaScript’s two ways of saying “no value”. The distinction is subtle but worth knowing.

  • undefined means a value has not been assigned. JavaScript produces it automatically.
  • null means “intentionally empty”. You assign it deliberately.
let x;
console.log(x);                 // undefined — never assigned

function greet(name) {
  console.log(`Hello, ${name}`);
}
greet();                        // Hello, undefined — argument not passed

const userPhoto = null;         // explicitly: no photo

A practical rule:

  • If you didn’t set it, it’s undefined.
  • If you set it on purpose to “nothing”, use null.

Many APIs and databases use null to mean “this field has been cleared”. Both compare equal to each other with == but not with === (more on that in operators).

bigint

For integers larger than 2^53 - 1 (around 9 quadrillion), JavaScript has a separate bigint type, written with an n suffix:

const big = 9007199254740993n;
const result = big + 1n;
console.log(result);            // 9007199254740994n

You will rarely need this in beginner code. It exists for cryptography, financial calculations, and anywhere precision matters more than convenience.

symbol

A symbol is a unique, anonymous value used mostly as a special kind of object key.

const id = Symbol("user-id");

You almost never declare a symbol explicitly in beginner code. We mention it only for completeness.

Object

Everything that isn’t a primitive is an object. The simplest object is a key/value collection inside braces:

const user = {
  name: "Ada",
  age: 30,
  isAdmin: false,
};

console.log(user.name);     // Ada
console.log(user.age);      // 30

Arrays are also objects:

const colors = ["red", "green", "blue"];
console.log(colors[0]);     // red
console.log(colors.length); // 3

Functions are objects too — they can be passed around, stored in variables, and given properties. We have dedicated posts coming up for arrays and objects.

Checking types with typeof

typeof is an operator that returns a string describing a value’s type:

console.log(typeof "hello");      // 'string'
console.log(typeof 42);           // 'number'
console.log(typeof true);         // 'boolean'
console.log(typeof undefined);    // 'undefined'
console.log(typeof 9007199254740993n); // 'bigint'
console.log(typeof Symbol("x"));  // 'symbol'
console.log(typeof { a: 1 });     // 'object'
console.log(typeof [1, 2, 3]);    // 'object'   — yes, arrays are objects
console.log(typeof function(){}); // 'function'

And the famous quirk:

console.log(typeof null);         // 'object'   — wrong, but historical

null reports as 'object' due to a bug from JavaScript’s first week of existence in 1995 that can never be fixed without breaking the web. Remember it once and move on.

Checking for null

Because of the typeof quirk, the reliable check is direct equality:

const value = null;
if (value === null) {
  console.log("value is null");
}

Checking for arrays

Use Array.isArray, not typeof:

console.log(Array.isArray([1, 2, 3]));   // true
console.log(Array.isArray("hello"));      // false

Try it yourself. In node, predict the typeof output for each of these before running:

console.log(typeof "");
console.log(typeof 0);
console.log(typeof null);
console.log(typeof undefined);
console.log(typeof NaN);
console.log(typeof []);
console.log(typeof {});

Type coercion: JavaScript converts for you

JavaScript will happily convert between types in many operations. Sometimes this is helpful; sometimes it is the cause of bugs that have launched a thousand blog posts.

String concatenation with +

If either operand of + is a string, JavaScript converts the other to a string:

console.log("Result: " + 42);     // 'Result: 42'
console.log("Score: " + true);    // 'Score: true'
console.log(1 + "1");             // '11'   — surprising
console.log(1 + 1 + "1");         // '21'   — left to right

This is exactly why we prefer template literals in modern code:

const score = 42;
console.log(`Score: ${score}`);   // unambiguous

Arithmetic operators convert to numbers

Other arithmetic operators (-, *, /, %) try to convert both sides to a number:

console.log("5" - 2);             // 3
console.log("5" * "2");           // 10
console.log("hello" - 2);         // NaN

Converting on purpose

When you want to convert, do it explicitly so the code’s intent is clear:

Number("42");        // 42
Number("hello");     // NaN
String(42);          // '42'
Boolean(0);          // false
Boolean("");         // false
Boolean("hello");    // true

Truthy and falsy

In a boolean context (an if, a while, the second operand of &&), every value counts as either “truthy” or “falsy”.

The falsy values are:

false, 0, -0, 0n, "", null, undefined, NaN

Everything else is truthy — including "false" (a non-empty string), [] (an empty array, somewhat surprisingly), and {} (an empty object).

if ("hello") console.log("truthy");
if ([]) console.log("yes, an empty array is truthy");
if (0) console.log("never prints");

This shortcut is heavily used:

function greet(name) {
  if (!name) {
    name = "stranger";
  }
  console.log(`Hello, ${name}`);
}

Try it yourself. Without running it, decide whether each of these prints "truthy" or "falsy":

for (const v of [0, "", "0", [], {}, null, undefined, NaN, "false"]) {
  console.log(v, "-", v ? "truthy" : "falsy");
}

Then run it. The "0" and [] cases catch nearly everyone the first time.

Recap

You now know:

  • The seven primitive types (string, number, boolean, null, undefined, bigint, symbol) and the one non-primitive umbrella (object)
  • undefined happens automatically; null is intentional
  • typeof is your everyday type check, but typeof null === 'object' is a quirk to memorise
  • Use Array.isArray() to check for arrays
  • JavaScript converts types automatically with +, -, and inside if conditions — and modern style prefers explicit conversions or template literals to avoid surprises
  • The falsy values are: false, 0, 0n, "", null, undefined, NaN

Next steps

We have covered every type briefly. Next, we go deep on the most-used one for calculation work: numbers, math operators, rounding, and the floating-point quirk every developer eventually meets.

→ Next: Numbers and Math in JavaScript

Questions or feedback? Email codeloomdevv@gmail.com.