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

1
2
3
4
5
6
7
var a;
var b = null;
var c = true;
var d = 123;
var e = "hello";
var f = {a:1};
// a, b, c, d, e are primitive types; f is a reference type

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

yinyongleixing

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.

1
2
var obj1 = {a:1};
var obj2 = obj1;

qubie

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
1
2
3
4
var a;
console.log(a);
console.log(b)
// Declared but not assigned
  • Accessing a property that doesn't exist on an object

    1
    2
    3
    var obj = {a:1,b:2};
    console.log(obj.c);
    // obj has no property c
  • The result of running a function that has no return value

1
2
3
4
5
function f(){
alert("test");
}
var ret = f();
console.log(ret);
  • A function parameter that wasn't passed in
    1
    2
    3
    4
    5
    functiom f(i,j){
    console.log(i);
    console.log(j);
    };
    f(1);

3. Type Conversion

undefined => Boolean, used when checking whether a property exists

1
2
3
4
5
var obj = {a:1,b:function(){},...};
if(obj.c){
console.log(obj.c);
}
// The result is false

undefined => Number

1
2
3
var count;
count + 1;
// The result is NaN

undefined => String

1
2
3
(function(a){
console.log("a:" + a);
})()

Null

1. Type Description

Its value is null

2. When It Appears

null is returned when an object doesn't exist
document.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
1
2
true
var a = true;

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

1
2
'hello world!'
var s = 'name = "myform"';

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

1
2
3
4
10
1.4
1.2e5
var count = 0x10;

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