Programming Search⌘K

Featureintro

Primitive values

Also called primitives, primitive types, primitive data types, immutable values.

A primitive is a value that is not an object: a string, number, bigint, boolean, undefined, symbol or null. Primitives are immutable, and === compares them by value.

Language
JavaScript

Synopsis

Primitive type Example Wrapper object
string 'cat' String
number 42 Number
bigint 42n BigInt
boolean true Boolean
undefined undefined none
symbol Symbol('id') Symbol
null null none

"In JavaScript , a primitive (primitive value, primitive data type) is data that is not an object and has no methods or properties . There are 7 primitive data types".1 The wrapper column is from MDN.2

let s = "cat";
s.toUpperCase();
console.log(s);
s = s.toUpperCase();
console.log(s, "abc" === "a" + "bc", {} === {});
Output
cat
CAT true false

Semantics

r1. Seven primitive types. "All types except Object define immutable values represented directly at the lowest level of the language. We refer to values of these types as primitive values."2 Example: synopsis.

r2. Immutable. "All primitives are immutable ; that is, they cannot be altered." "The language does not offer utilities to mutate primitive values."1 A string method such as toUpperCase() returns a different string, and the first string stays as it was. "JavaScript strings are immutable."2 Example: synopsis.

r3. The variable is not the value. "It is important not to confuse a primitive itself with a variable assigned a primitive value. The variable may be reassigned to a new value, but the existing value cannot be changed".1 Example: copy.

r4. Methods through wrappers. "Primitives have no methods but still behave as if they do. When properties are accessed on primitives, JavaScript auto-boxes the value into a wrapper object and accesses the property on that object instead."1 null and undefined have no wrapper.2 See Primitive wrapper objects.

r5. Writes to a primitive are lost. str.foo = 1 "is not assigning to the property foo of str itself, but to an ephemeral wrapper object".1 For a string index, an attempt "to delete or assign a value to these properties will not succeed. The properties involved are neither writable nor configurable."3 In sloppy mode the write fails with no error. In strict mode it throws a TypeError. Example: lost-write and strict-write.

r6. Comparison by value. For ===, "Numbers must have the same numeric values", "Strings must have the same characters in the same order" and "Booleans must be both true or both false". For objects, it returns true "only if they refer to the same object."4 Example: compare.

Examples

The examples run with Node.js 24.

A primitive copies as a value. An object is shared between two variables (rule r3):

let a = 1;
let b = a;
b += 1;
console.log(a, b);

const o = { n: 1 };
const p = o;
p.n += 1;
console.log(o.n, p.n);
Output
1 2
2 2

A write to a primitive has no effect in sloppy mode (rule r5):

const str = "cat";
str.foo = 1;
str[0] = "b";
console.log(str, str.foo, str[0]);
Output
cat undefined c

The same write to a string index throws in strict mode (rule r5):

This code raises TypeError, on purpose.

"use strict";
const str = "cat";
str[0] = "b";

Primitives compare by value, objects by identity (rule r6):

console.log("hello" === "hello", 3 === 3, 1n === 1n, true === true);
console.log([1] === [1], { a: 1 } === { a: 1 }, Symbol("x") === Symbol("x"));
Output
true true true true
false false false

Two calls to Symbol('x') give two different symbols, so a symbol is equal only to itself.

Pitfalls

Calling a method does not change the string

s.trim(), s.toUpperCase() and s.replace() return a value. Assign the result, as in s = s.toUpperCase() (rule r2).

Silent failed writes

In sloppy mode, str[0] = 'b' does nothing and gives no error (lost-write). Use strict mode to get the TypeError (rule r5).

Comparison

Topic JavaScript Python
values that are not objects seven primitive types1 none, "All data in a Python program is represented by objects"5
immutable numbers yes1 "Numeric objects are immutable"5
immutable strings yes2 "An object of an immutable sequence type cannot change once it is created."5
methods on a value through an auto-boxed wrapper1 on the object itself5

In Python a number and a string are objects, and each has a type and an identity.5 JavaScript has no object behind a primitive, so it creates a wrapper for each property read (rule r4). See Objects, values and types.

References

  1. Primitive, MDN Glossary, introduction
  2. JavaScript data types and data structures, MDN, Primitive values; String type; Objects
  3. String, MDN, Character access
  4. Strict equality (===), MDN, Description
  5. Data model, The Python Language Reference, 3.1 Objects, values and types; 3.2.4 numbers.Number; 3.2.5.1 Immutable sequences

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source