JavaScript Primitive Types
Primitive values are the simplest values in JavaScript.
Primitive values are not objects.
JavaScript has seven primitive types.
The Primitive Types
| Type | Example | typeof Result |
|---|---|---|
| string | "Hello" |
"string" |
| number | 123 |
"number" |
| boolean | true |
"boolean" |
| bigint | 123n |
"bigint" |
| symbol | Symbol() |
"symbol" |
| undefined | undefined |
"undefined" |
| null | null |
"object" |
The result of typeof null is "object".
This is a historical JavaScript behavior.
Primitive Values Are Immutable
Primitive values cannot be changed.
Operations on primitive values create new values.
Example
let text = "Hello";
text = text + " World";
The original string is not modified.
A new string is created and assigned to the variable.
Primitive Values Are Not Objects
Primitive values are not objects.
Primitive values do not have their own properties.
Example
let text = "Hello";
document.getElementById("demo").innerHTML =
typeof text;
The result is:
string
Wrapper Objects
JavaScript provides wrapper objects for some primitive types.
| Primitive Type | Wrapper Object |
|---|---|
| string | String |
| number | Number |
| boolean | Boolean |
Primitive values can use methods provided by their wrapper objects.
Example
let text = "Hello";
document.getElementById("demo").innerHTML =
text.toUpperCase();
Primitive Types Overview
String
Strings represent text values.
"Hello"
'Hello'
`Hello`
Learn more about string values
Number
Numbers represent numeric values.
123
3.14
-10
Learn more about number values
Boolean
Booleans represent true or false.
true
false
Learn more about boolean values
BigInt
BigInt values represent large integers.
123n
9007199254740993n
Learn more about bigint values
Symbol
Symbols represent unique identifiers.
Symbol()
Symbol("id")
Learn more about symbol values
Undefined
Undefined represents the absence of a value.
let x;
Null
Null represents the intentional absence of a value.
let x = null;
Primitive Types vs Object Types
| Primitive Types | Object Types |
|---|---|
| string | Object |
| number | Array |
| boolean | Function |
| bigint | Date |
| symbol | RegExp |
| undefined | Error |
| null | Map |
Primitive values are not objects.
Object types inherit from Object.