Primitive Data Types

What Are the 7 Primitive Data Types in JavaScript?

The seven primitive data types in JavaScript: string, number, bigint, boolean, undefined, symbol and null, with examples and how they differ from C++.

On this page
  1. The seven primitive types
  2. Primitives cannot be changed in place
  3. Checking the type with typeof
  4. One number type for integers and decimals
  5. How this differs from C++

JavaScript has seven primitive data types: string, number, bigint, boolean, undefined, symbol and null. Every value that is not a primitive is an object, including arrays and functions. This article covers the seven with examples, then ends with how they differ from the C++ types you may already know.

The seven primitive types

const title = "Learning C++";              // string
const version = 23;                         // number
const largeCount = 12345678901234567890n;   // bigint
const isOpen = true;                        // boolean
let pending;                                // undefined
const token = Symbol("id");                 // symbol
const reset = null;                         // null
  • string is text, written in single, double or backtick quotes.
  • number covers integers and decimals. It is stored as a 64-bit floating-point value.
  • bigint is for whole numbers that may be too large for the number type to hold exactly. Add an n at the end of the literal.
  • boolean is true or false.
  • undefined is the value a declared variable holds before you assign it one.
  • symbol is a unique value. Each call to Symbol() returns a new one.
  • null is a value you assign on purpose to mean “no value here”.

Primitives cannot be changed in place

Strings are immutable, so you cannot change one character by assigning to its index:

const word = "cat";
word[0] = "b";
console.log(word); // still "cat"

Outside strict mode, the assignment is silently ignored. In strict mode, it throws a TypeError.

Checking the type with typeof

typeof "hello";       // "string"
typeof 42;            // "number"
typeof 42n;           // "bigint"
typeof true;          // "boolean"
typeof undefined;     // "undefined"
typeof Symbol("id");  // "symbol"
typeof null;          // "object"

The last line is a long-standing quirk. typeof null returns "object", a bug that dates back to early JavaScript and is kept for compatibility. To test for null, compare with === null. Use === in general for comparisons, because it checks the type as well as the value.

One number type for integers and decimals

C++ has separate integer types, such as int and long long, and floating-point types, such as float and double. JavaScript’s number type covers both, and it stores every value as a 64-bit floating-point number. That has consequences for large values and for decimals:

console.log(0.1 + 0.2 === 0.3);                      // false
console.log(Number.MAX_SAFE_INTEGER);                // 9007199254740991
console.log(9007199254740992 === 9007199254740993);  // true: 9007199254740993 cannot be stored exactly

Number.MAX_SAFE_INTEGER is the largest whole number up to which every integer is stored exactly. For bigger whole numbers, use bigint.

How this differs from C++

  • Types are fixed in C++. A variable declared as int stays an int. In JavaScript, a variable declared with let can hold a string at one point and a number later.
  • Number types. A C++ int is an integer with a fixed size, usually 32 bits. JavaScript’s number covers integers and decimals alike, and bigint is needed for exact large integers.
  • Characters. A C++ char is a small integer type. JavaScript has no character type. A single character is a string of length 1.
  • Missing values. In C++, reading an uninitialized local variable is undefined behavior. In JavaScript, a declared variable with no value is undefined, which is a defined result.
  • Null. A C++ nullptr is a pointer value. JavaScript’s null is a general “no value” primitive.
  • When types are checked. C++ checks types at compile time. JavaScript checks types while the program runs, and typeof lets you inspect them.

JavaScript also converts values between types on its own much more often than C++ does. That automatic conversion is called coercion, and it is different from the explicit casts described in what is type casting in C.

The seven primitives cover the simple values in JavaScript: text, numbers, large integers, true or false, missing values, unique identifiers and intentional empty values. Knowing how each one behaves, especially number precision and null, prevents many surprises when you move between JavaScript and C++.

The complete guide to primitive data types

Primitive Data Types in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

The Latest C++ Standard

C++23 is the most recent C++ standard at the time of writing, with C++26 next. Here is what changed, how versions are named, and how to compile for each.

C++ Standards 3 min read