An introduction to the 6 basic types (Undefined, Null, Boolean, Number, String, Object), plus primitive and reference types.
Standard Types
Standard types are divided into primitive types and reference objects. Primitive types include
- Undefined
- Null
- Boolean
- String
- Number
- Object
Reference Types
- Object
How Primitive and Reference Types Differ in Stack Memory
|
|
Primitive types in stack memory
| a |undefined(Undefined type)|
|—|—:|
|b | null(Null type)|
|c | true(Boolean type)|
|d | 123(Number type)|
|e | "hello"(String type)|
|f | Object type|
Primitive values are stored directly in stack memory, while a reference type stores only an address in stack memory. That address points to heap memory, as shown below

From the diagram, you can see that copying a primitive copies the value in stack memory, while copying a reference type only copies the address pointing to heap memory, as shown below.
|
|

Here's a detailed look at how primitive types work
Undefined
1. Type Description
This type has one value: undefined
2. When It Appears
- A variable that has been declared but not assigned
|
|
Accessing a property that doesn't exist on an object
123var obj = {a:1,b:2};console.log(obj.c);// obj has no property cThe result of running a function that has no return value
|
|
- A function parameter that wasn't passed in12345functiom f(i,j){console.log(i);console.log(j);};f(1);
3. Type Conversion
undefined => Boolean, used when checking whether a property exists
|
|
undefined => Number
undefined => String
Null
1. Type Description
Its value is null
2. When It Appears
null is returned when an object doesn't existdocument.getElementById('notExistElement');
3. Type Conversion
null => Boolean: the result is false
null => Number: the result is 0
null => String: the result is "null"
Boolean
1. Type Description
Its values are true and false
2. When It Appears
- Implicit type conversion performed by conditional statements
if(document.getElementById('notExistElement')){...}else{...}
When the element doesn't exist, null is returned, the condition implicitly converts it to false, and the else branch runs. - Literals or variable definitions
|
|
3. Type Conversion
| Conversion | Number | String |
|---|---|---|
| true | 1 | “null” |
| false | 0 | “false” |
String
1. Type Description
A sequence of characters wrapped in single or double quotes
2. When It Appears
|
|
3. Type Conversion
| Conversion | Number | Boolean |
|---|---|---|
| “” | 0 | false |
| “123” | 123 | true |
| “1a” | true | NaN |
Number
1. Type Description
Integer literals, octal (0) literals, hexadecimal (0x) literals, and floating-point literals
2. When It Appears
|
|
3. Type Conversion
| Conversion | String | Boolean |
|---|---|---|
| 0 | “0” | false |
| 1 | “1” | true |
| Infinity | “Infinity” | true |
| NaN | “NaN” | false |
Object
1. Type Description
The Object type is a collection of properties
2. When It Appears
var obj = {name:'jerry', age: 0};
3. Type Conversion
| Conversion | String | Boolean | Number |
|---|---|---|---|
| {} | “[object Object]” | true | NaN |
