1. What Are Primitive Types and Object Types?
Primitive types and object types are the two major categories JavaScript uses when classifying data types.
The most important difference between the two lies in the semantic model of the data:
- Primitive types: Represent immutable primitive values; variable assignment and passing exhibit value semantics.
- Object types: Represent data with object identity and a property structure; variables typically access objects by reference.
Note that the primitive/object distinction is not a fixed classification shared by all programming languages. JavaScript, Python, Java and other languages each have their own object models and type systems.
This article focuses mainly on JavaScript, using other languages to illustrate the relevant concepts.
2. JavaScript's Data Type System
JavaScript uses a dynamic type system: variables themselves have no fixed data type, and the type is determined by the value currently bound to them.
JavaScript has 8 main data types:
| Category | Data Type | Description |
|---|---|---|
| Primitive | string | String |
| Primitive | number | Numeric value, including integers and floating-point numbers |
| Primitive | bigint | Arbitrary-precision integer |
| Primitive | boolean | Boolean value |
| Primitive | undefined | Undefined value |
| Primitive | symbol | Unique identifier |
| Primitive | null | Null value |
| Object | object | Objects and their derived types |
Among them, functions are also a special form of object, with the ability to be called.
JavaScript's type system can be summarized as:
flowchart TD
A["JavaScript Data Types"] --> B["Primitive Types"]
A --> C["Object Types"]
B --> D["string"]
B --> E["number"]
B --> F["bigint"]
B --> G["boolean"]
B --> H["undefined"]
B --> I["symbol"]
B --> J["null"]
C --> K["Plain objects"]
C --> L["Arrays"]
C --> M["Functions"]
C --> N["Date, RegExp, and other objects"]
C --> O["Collection objects such as Map and Set"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style D fill:#E0F0FE,stroke:#93BCE8,color:#334155
style E fill:#E0F0FE,stroke:#93BCE8,color:#334155
style F fill:#E0F0FE,stroke:#93BCE8,color:#334155
style G fill:#E0F0FE,stroke:#93BCE8,color:#334155
style H fill:#E0F0FE,stroke:#93BCE8,color:#334155
style I fill:#E0F0FE,stroke:#93BCE8,color:#334155
style J fill:#E0F0FE,stroke:#93BCE8,color:#334155
style K fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style L fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style M fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style N fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style O fill:#F3EDFC,stroke:#C4B2E3,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
You can check the type of a value with typeof:
There are two results here that deserve special attention:
```javascript typeof null; // "object" typeof []; // "object" ```typeof null returning "object" is a historical legacy behavior in JavaScript; it does not mean that null is really of the object type.
Arrays also belong to the object type, and can be checked with Array.isArray().
III. Primitive Types
1. Core Characteristics of Primitive Types
Primitive types represent the basic data values provided by the language.
They have the following characteristics:
- They have no internal state that users can directly modify.
- Assignment follows value-copy semantics.
- They do not have the independent identity of objects.
- They can be used as function parameters and return values.
- Property access syntax can trigger the temporary wrapper mechanism, but the primitive value itself is still not an object.
For example:
```javascript let a = 10; let b = a; b = 20; console.log(a); // 10 console.log(b); // 20 ```Variable b receives the value 10, and reassigning it afterwards does not affect a.
2. string: Strings
Strings are used to represent text data.
```javascript let name = "Alice"; let upper = name.toUpperCase(); console.log(name); // Alice console.log(upper); // ALICE ```JavaScript strings are immutable.
Calling toUpperCase() does not modify the original string; it returns a new string value.
Even if you try to modify a character through an index, the original string will not change.
Note that JavaScript strings are based on UTF-16 encoding, and their length returns the number of UTF-16 code units, not the number of characters as users intuitively perceive them.