AlgoInsight

Free lesson · JavaScript Basic to Advance

Values and Types: The Foundation

Easy5 min read

What Is a Value, and What Is a Type?

Every piece of data in a JavaScript program is a value. The number 42 is a value. The text "Hello" is a value. The boolean true is a value. Even the absence of a value — null — is itself a value.

Every value has a type. The type tells JavaScript what the value is and what you can do with it. 42 has the type number. "Hello" has the type string. true has the type boolean. The type determines the rules: you can add two numbers, you can join two strings, you can compare two booleans. Mixing types causes specific behaviors you need to understand.

If you take one thing from this Topic, take this: JavaScript has exactly seven primitive types, plus one category for everything else. Once you know those, you know every kind of value you'll ever see in JavaScript.

Why Types Matter in JavaScript

JavaScript is what's called a dynamically typed language. That means you don't declare what type a variable will hold before you use it. You just assign a value, and JavaScript figures out the type.

let x = 42;          // JavaScript decides x is a number
console.log(typeof x);

x = "now I'm a string";  // Now JavaScript decides x is a string
console.log(typeof x);

x = true;            // Now it's a boolean
console.log(typeof x);

The same variable holds three different types in three lines. JavaScript doesn't complain. This flexibility is convenient — but it's also the source of most type-related bugs in JavaScript code. A variable you expected to be a number is suddenly a string, your math breaks, and you don't find out until production.

This is why understanding types deeply matters even though JavaScript doesn't force you to think about them upfront. The language hides the type system from you. You have to learn it consciously.

The Seven Primitive Types

A primitive value is a simple, single value. It has no methods of its own (though JavaScript wraps it temporarily to fake some). It's immutable — you can't change it, only replace it. JavaScript has seven primitive types:

graph TD
    A[JavaScript Primitive Types] --> B[string<br/>'Hello']
    A --> C[number<br/>42, 3.14]
    A --> D[boolean<br/>true, false]
    A --> E[null<br/>intentional emptiness]
    A --> F[undefined<br/>not yet assigned]
    A --> G[symbol<br/>unique identifiers]
    A --> H[bigint<br/>huge integers]

    style A fill:#7c3aed,color:#fff
This diagram shows the seven primitive types in JavaScript. They are: string for text values, number for numeric values including decimals, boolean for true or false, null which represents intentional emptiness, undefined which represents a variable that has not yet been assigned, symbol for unique identifiers, and bigint for integers larger than what number can safely hold.

Let me walk through each one briefly. Each gets a deeper Topic later in this module.

string — text. Wrapped in quotes (single, double, or backticks). "Olena", 'hello', `template literal`.

number — any numeric value, integer or decimal. 42, 3.14, -7, 0. Notably, JavaScript doesn't distinguish integers from decimals at the type level — they're all just number.

boolean — exactly two values, true and false. Used for logic, conditionals, and flags.

null — represents intentional emptiness. You wrote null because you wanted to say "this slot is deliberately empty."

undefined — represents the absence of an assigned value. JavaScript uses this automatically when a variable exists but hasn't been given a value yet, or when a function doesn't return anything.

symbol — unique identifiers. You'll rarely use these directly as a beginner, but they exist. Two symbols are never equal to each other, even if created with the same description. They're useful for keys that can't collide.

bigint — integers larger than number can safely represent. JavaScript's number type can only handle integers up to about 9 quadrillion safely. For anything larger, you use bigint, written with an n suffix: 9007199254740993n.

Everything Else Is an Object

Anything that isn't one of those seven primitive types is an object. That's not a typo — JavaScript treats arrays as objects, functions as objects, dates as objects. The object category is huge and contains most of the interesting things you'll work with.

console.log(typeof "Olena");           // "string"
console.log(typeof 42);                // "number"
console.log(typeof true);              // "boolean"
console.log(typeof undefined);         // "undefined"
console.log(typeof Symbol("id"));      // "symbol"
console.log(typeof 100n);              // "bigint"

console.log(typeof {name: "Olena"});   // "object"
console.log(typeof [1, 2, 3]);         // "object" (arrays are objects)
console.log(typeof function() {});     // "function" (functions are a special case)
console.log(typeof null);              // "object" (this is a famous bug)

Notice two things at the bottom of that output. Functions report as "function" even though they're technically a kind of object — JavaScript treats them specially. And typeof null returns "object", which is wrong. null is not an object. It's a primitive value. But this bug has existed since 1995 and can never be fixed because too much existing code depends on the wrong answer.

This is the kind of weirdness Module 1 prepared you for. Ten days of design. Locked in forever.

Interactive animation: Primitives beside objects - seven primitive types and one object category, but only two behaviours.

How to Check Types in Practice

The typeof operator is your first tool for checking types. It works for primitives but has limitations with objects:

const name = "Olena";
const age = 35;
const isFounder = true;
const company = { name: "Microsoft", role: "Head of Product" };
const skills = ["TypeScript", "React", "Node.js"];

console.log(typeof name);       // "string"
console.log(typeof age);        // "number"
console.log(typeof isFounder);  // "boolean"
console.log(typeof company);    // "object"
console.log(typeof skills);     // "object" — but you probably wanted "array"!

typeof can't tell you that skills is specifically an array — it just says "object." For more precise type checks, you'll learn other tools later: Array.isArray(), instanceof, and various pattern-matching techniques. But for primitive types, typeof is what you'll reach for ninety percent of the time.

Interactive animation: What typeof returns for six values, row by row - and the two rows, null and an array, where it misleads.

Common Pitfalls

Warning: Three things about types that trip up new JavaScript developers.

typeof null returns "object", not "null". This is a bug from the original 1995 implementation. Never trust typeof to detect null. Use value === null instead.

JavaScript doesn't distinguish integers from floating-point numbers. Both 5 and 5.0 are the same number type. If you need to know whether a number has a decimal part, you have to check manually with something like Number.isInteger(value).

undefined is not the same as null. They're two different primitive types. undefined means "no value has been assigned yet." null means "I deliberately put nothing here." Beginners often treat them interchangeably. Interviewers will catch this and use it as a signal that you don't yet understand JavaScript deeply.

Why This Matters in Interviews

Interview: "How many primitive types does JavaScript have?" is a common warm-up question. The answer is seven. Many candidates say five or six because they forget about symbol or bigint — the two newest additions. Knowing all seven, and being able to briefly explain each one, signals that you've actually studied the language rather than picked it up by osmosis.

Topic 2.8 — Interview Lens: Type Coercion Questions — covers the deeper type-related interview questions, including the famous "what does typeof null return and why" question and the type coercion puzzles that come up constantly in JavaScript interviews.

Try It

Exercise: Open a JavaScript console and run typeof on each of these values. Predict the output for each before you run it.

typeof "42"
typeof 42
typeof true
typeof undefined
typeof null
typeof []
typeof {}
typeof function() {}
typeof NaN
typeof 100n

Two of these will surprise you. Note which ones. By the end of this module, you'll understand why each one returns what it does, and the deeper reasoning behind JavaScript's type system.

Back to JavaScript Basic to Advance