Data Types and Values

Lesson 2 of 10

On this page

Every value in JavaScript has a type — it tells the language (and you) what kind of thing you’re working with, and what you can do with it. You can’t add a number to a boolean the same way you add two numbers, and you can’t call .toUpperCase() on a number. Knowing the types you’ll encounter makes the rest of the syntax in this series click into place.

JavaScript’s types fall into two groups: a handful of simple primitive types that hold a single value, and object types that group many values together. We’ll look at the ones you’ll meet constantly first.

Strings — text

A string is a sequence of characters, used for any kind of text.

const name = "Ada Lovelace";
const initial = 'A';
const bio = `${name} was a mathematician.`;

Strings can be wrapped in double quotes, single quotes, or backticks — the backtick form is called a template literal and lets you embed values directly inside the text with ${ }. The dedicated Strings lesson later in this series covers all of this in depth.

Numbers — every real number

JavaScript has a single number type for both whole numbers and decimals — there’s no separate “integer” type to worry about.

const quantity = 4;
const price = 19.99;
const negative = -12;
const scientific = 1.5e3; // 1500

Booleans — true or false

A boolean is one of exactly two values, true or false. They’re the backbone of decisions and comparisons, which the Conditionals lesson covers.

const isLoggedIn = true;
const hasDiscount = false;
const isAdult = 21 >= 18; // true

Objects — grouped name/value pairs

An object groups related values together under named properties. They are JavaScript’s go-to way to represent “a thing with attributes,” like a user or a product.

const user = {
    name: "Grace Hopper",
    role: "admiral",
    active: true,
};

console.log(user.name); // "Grace Hopper"

The Objects lesson later in this series is dedicated entirely to working with these.

Arrays — ordered lists of values

An array is an ordered, numbered list of values — perfect for collections like a shopping cart or a list of names.

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

Arrays get their own lesson too, right after Strings.

undefined and null — “nothing,” two ways

JavaScript has two values that represent the absence of something, and they mean slightly different things:

let nickname;
console.log(nickname); // undefined — never assigned

let middleName = null;
console.log(middleName); // null — explicitly set to "nothing"

Checking a value’s type with typeof

When you’re not sure what type a value is — often because it came from user input, a network response, or a function you didn’t write — the typeof operator tells you:

typeof "hello";      // "string"
typeof 42;           // "number"
typeof true;         // "boolean"
typeof undefined;    // "undefined"
typeof { };          // "object"
typeof [1, 2, 3];    // "object" — arrays are a kind of object
typeof null;         // "object" — a famous, long-standing quirk

That last line is worth remembering: typeof null returns "object", even though null isn’t really an object. It’s a bug that shipped in the very first version of JavaScript and can never be fixed without breaking the web, so it’s just something to know about rather than something to use.

Lesser-used types worth recognizing

Two more primitive types exist for specialized situations. You’re unlikely to reach for these as a beginner, but you may see them:

The old way of doing things

Early JavaScript code sometimes wrapped primitive values in their object forms using new, hoping to get extra features:

const oldStyleString = new String("hello");
const oldStyleNumber = new Number(42);

typeof oldStyleString; // "object" — not "string"!

This created an object that behaved like a string or number in most situations, but not all — typeof reports "object" instead of "string", and comparisons can behave unexpectedly. It also costs more memory for no real benefit. Modern style avoids new String(...), new Number(...), and new Boolean(...) entirely and just writes the primitive value directly — "hello", 42, true — which is simpler, faster, and behaves the way you’d expect.