Types: number, string, boolean and the rest

Every value in JavaScript has a type, and there are eight of them. Seven are primitives: number, string, boolean, null, undefined, bigint and symbol. The eighth is object, which covers everything built from values, including arrays and functions. A variable has no type of its own, so the type is a property of the value that happens to be in the binding at that moment.

typeof is the operator that names the type, and it has one famous answer that looks like a mistake: typeof null is "object". This lesson prints the whole table, then the parts that cause real bugs: the floating point of numbers, the finite range of a safe integer, and the difference between converting and parsing text.

The eight types

The program below puts one value of each type in a pair with its name, then prints the name and what typeof says. The loop uses destructuring, which takes a pair apart into two bindings; the lesson on pattern matching in the later part covers the form in full.

const values = [ ['number', 42], ['string', 'text'], ['boolean', true], ['null', null], ['undefined', undefined], ['bigint', 42n], ['symbol', Symbol('id')], ['object', { name: 'object' }], ]; for (const [label, value] of values) { console.log(label + ' -> ' + typeof value); }

node types.js

number -> number
string -> string
boolean -> boolean
null -> object
undefined -> undefined
bigint -> bigint
symbol -> symbol
object -> object

Seven answers match their names, and null does not: it is a primitive, and the answer is left over from the first version of the language. A function also returns "function" from typeof, although a function is an object. Check for null with value === null, never with typeof.

Numbers and the safe range

There is one number type, and it is a 64-bit floating point value, so 1 and 1.5 are the same type. The format represents integers exactly up to 2^53 - 1, which is Number.MAX_SAFE_INTEGER. Beyond that, two different integers may share one value, and a check that compares a number with itself plus one starts to answer true.

bigint is the integer type without that limit: the literal ends in n, and a bigint and a number may not be mixed in arithmetic. The program prints the three facts worth remembering about the number type: the repeating decimal, the sum that is not 0.3, and the point where integers stop being exact.

console.log('1 / 3 =', 1 / 3); console.log('0.1 + 0.2 =', 0.1 + 0.2); console.log('safe integer:', Number.MAX_SAFE_INTEGER); console.log('safe + 1 equals safe + 2:', Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2); console.log('bigint:', 9007199254740993n); console.log('NaN:', Number.isNaN(0 / 0)); console.log('Infinity:', 1 / 0);

node numbers.js

1 / 3 = 0.3333333333333333
0.1 + 0.2 = 0.30000000000000004
safe integer: 9007199254740991
safe + 1 equals safe + 2: true
bigint: 9007199254740993n
NaN: true
Infinity: Infinity

Both decimal surprises come from the same cause: 0.1 and 0.2 have no exact form in binary floating point, so the sum is the nearest representable value. Compare money in cents, or round before you compare, and reach for bigint only when the integers are genuinely larger than the safe range.

Strings and conversion

A string is a sequence of UTF-16 code units, and length counts those units, not the characters a reader sees. Most methods return a new string rather than change the old one: toUpperCase, slice and replace are all pure. A template literal builds text from values, and the + operator does the same with two strings.

Conversion is explicit with Number and String, and strict: a string that is not entirely a number becomes NaN. Parsing with parseInt is looser and reads the digits it finds from the start, which is why it turns "42px" into 42. The second argument is the base, and it should always be written.

const text = 'JavaScript'; console.log('length:', text.length); console.log('upper:', text.toUpperCase()); console.log('slice:', text.slice(0, 4)); console.log('includes:', text.includes('Script')); console.log('template:', `I write ${text}`); console.log('Number("42"):', Number('42')); console.log('Number("42px"):', Number('42px')); console.log('parseInt("42px"):', parseInt('42px', 10));

node strings.js

length: 10
upper: JAVASCRIPT
slice: Java
includes: true
template: I write JavaScript
Number("42"): 42
Number("42px"): NaN
parseInt("42px"): 42

Number("42px") is NaN, and Number.isNaN is the way to test it; a comparison with NaN is false even when both sides are NaN. The lesson on equality shows why, and the one on strings later in the course goes past length to code points.

null, undefined and symbols

undefined is what a name holds before it is assigned and what a function returns when it returns nothing. null is a value a programmer writes to mean "no object here". The two are different values with different types, and loose equality is the only comparison that treats them as equal.

A symbol is a unique name. Two symbols with the same description are different values, which makes a symbol safe as a property key that cannot collide with another library. The last program prints what a symbol shares with its twin and what it does not.

const first = Symbol('id'); const second = Symbol('id'); console.log('description:', first.description); console.log('same description:', first.description === second.description); console.log('same symbol:', first === second); console.log('typeof:', typeof first);

node symbols.js

description: id
same description: true
same symbol: false
typeof: symbol

The description is text for a person, and equality is identity: no second call to Symbol produces the same value. The objects part of the course uses a symbol as a key that a loop over the object does not see.

Exercises

  1. Add an entry for a function to types.js. Which string does typeof return, and is a function a primitive?
  2. Print Number.MAX_SAFE_INTEGER + 2 === Number.MAX_SAFE_INTEGER + 3. What does the answer say about integers beyond the safe range?
  3. Change the base in the last line of strings.js from 10 to 16 and predict the three lines that change. Does the parse of "42px" still stop at the same place?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. How many types do JavaScript values have?

    Answer

    Eight

    Seven primitives plus object: number, string, boolean, null, undefined, bigint, symbol and object.

  2. What does typeof null return?

    Answer

    "object"

    The answer is a leftover from the first version of the language, and null must be tested with === null.

  3. What is the largest integer the number type represents exactly?

    Answer

    Number.MAX_SAFE_INTEGER, which is 2^53 - 1

    Beyond 2^53 - 1 two different integers may share one value, and bigint is the type without the limit.

  4. What does this program print? console.log(0.1 + 0.2);

    Answer

    0.30000000000000004

    Neither 0.1 nor 0.2 has an exact binary form, so the sum is the nearest representable value.

  5. What is the type of 42n?

    Answer

    bigint

    The n suffix makes a bigint, a separate type that may not be mixed with number in arithmetic.

  6. What does this program print? console.log(Number("42px"));

    Answer

    NaN

    Number requires the whole string to be a number; parseInt is the function that reads a prefix.

  7. What does parseInt("42px", 10) return?

    Answer

    42

    parseInt reads the digits from the start and stops at the first character that is not one.

  8. What is the difference between null and undefined?

    Answer

    null is a value a programmer writes, undefined is the absence of an assigned value

    Both exist, both have their own type, and only loose equality treats them as equal.

  9. What does this program print? const first = Symbol("id"); const second = Symbol("id"); console.log(first === second);

    Answer

    false

    Every call to Symbol creates a new unique value, and the description is not part of its identity.

  10. What does typeof return for an array?

    Answer

    "object"

    An array is an object; Array.isArray is the check that tells it apart from other objects.

The code on this page was run with Node.js 24.22.1 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev