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
nat the end of the literal. - boolean is
trueorfalse. - 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
intstays anint. In JavaScript, a variable declared withletcan hold a string at one point and a number later. - Number types. A C++
intis an integer with a fixed size, usually 32 bits. JavaScript’snumbercovers integers and decimals alike, andbigintis needed for exact large integers. - Characters. A C++
charis 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++
nullptris a pointer value. JavaScript’snullis a general “no value” primitive. - When types are checked. C++ checks types at compile time. JavaScript checks types while the program runs, and
typeoflets 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++.