Easy Tutorial
❮ Python Ten Minute Introductory Tutorial Cpp Construct Function Initial List ❯

JavaScript Primitive Types and Reference Types, Simple Assignment, Object Reference

Category Programming Techniques

ECMAScript variables come in two different data types: primitive types and reference types. They are also known as primitive and object types, types with methods and types without methods, and can also be divided into mutable and immutable types. In fact, these terms are all named according to the characteristics of these types, so you can call them whatever you like o(╯□╰)o.

1. Primitive Types

The basic data types include: undefined, boolean, number, string, null. Primitive types are accessed by value, which means you can operate on the actual value stored in the variable. Primitive types have the following characteristics:

1. The values of primitive types are immutable:

No method can change the value of a primitive type, such as a string:

var name = 'jozo';
name.toUpperCase(); // Outputs 'JOZO'
console.log(name); // Outputs 'jozo'

It is found that the original name has not changed, but after calling the toUpperCase() method, a new string is returned.

var person = 'jozo';
person.age = 22;
person.method = function(){//...};

console.log(person.age); // undefined
console.log(person.method); // undefined

From the above code, it can be seen that we cannot add properties and methods to primitive types, once again showing that primitive types are immutable;

2. Comparison of primitive types is by value:

They are only equal when their values are equal.

var a = 1;
var b = true;
console.log(a == b); // true

Aren't they equal? In fact, this is the knowledge of type conversion and the == operator, which means that when comparing two variables of different types with ==, some type conversion will be performed. For example, the above comparison first converts true to the number 1 and then compares it with the number 1, resulting in true. This is when the == operator performs type conversion when comparing two values of different types, but when the two values being compared are of the same type, even == is equivalent to ===.

var a = 'jozo';
var b = 'jozo';
console.log(a === b); // true

3. Primitive type variables are stored in the stack area (the stack area refers to the stack memory in memory)

Suppose there are the following primitive type variables:

var name = 'jozo';
var city = 'guangzhou';
var age = 22;

Then its storage structure is as shown in the figure below:

The stack area includes the variable's identifier and the value of the variable.

2. Reference Types

Reference types are more interesting.

In JavaScript, in addition to the basic types mentioned above (number, string, boolean, null, undefined), there are reference types, which can also be said to be objects. An object is a collection of properties and methods. That is to say, reference types can have properties and methods, and properties can include both primitive and reference types. Let's look at some characteristics of reference types:

1. The values of reference types are mutable

We can add properties and methods to reference types, and we can also delete their properties and methods, such as:

var person = {}; // Create an empty object -- Reference type
person.name = 'jozo';
person.age = 22;
person.sayName = function(){console.log(person.name);}
person.sayName(); // 'jozo'

delete person.name; // Delete the name property of the person object
person.sayName(); // undefined

The above code shows that reference types can have properties and methods, and they can be dynamically changed.

2. The values of reference types are objects saved in both stack memory and heap memory

JavaScript is different from other languages, it does not allow direct access to the location in memory, which means you cannot directly manipulate the memory space of an object. So what do we operate on? In fact, it is the reference to the object that is operated on, so the value of the reference type is accessed by reference.

var person1 = {name:'jozo'};
var person2 = {name:'xiaom'};
var person3 = {name:'xiaoq'};

The storage of these three objects in memory is shown in the figure below:

3. Comparison of reference types is by reference

var person1 = '{}';
var person2 = '{}';
console.log(person1 == person2); // true

When talking about the comparison of primitive types, it was

❮ Python Ten Minute Introductory Tutorial Cpp Construct Function Initial List ❯