js data type storage difference

type of data:

1. The data types of JavaScript (hereinafter referred to as js) are divided into two types: basic data types and reference data types

2. Basic data types: number (array) String (string) boolean (Boolean) null (empty) undefined (undefined) symbol bigint

3. Reference data types: object (Object), array (Array), function (Function), and two special objects: regular (RegExp) and date (Date)

Storage difference:

Primitive data types and reference data types are stored differently in memory:

1. Basic data types are stored on the stack

2. Reference data types are stored in the heap

When we assign a value to a variable, the first thing the parser needs to confirm is whether the value is a basic type or a reference type

basic data type

let a='111';
let b=a;
b='222';
console.log(a);//111
console.log(b);//222
//理解:a的值是一个基本数据类型,是存储在栈中,
//将a的值赋给b,虽然两个变量的值相等,
//但是两个变量形成了两个不同的内存地址 所以b发生改变不会影响到a

reference data type

let c={name:'好先森'};
let d=c;
d.name='坏先森'
console.log(c);//坏先森
console.log(d);//坏先森
//理解:引用类型的数据是放在堆中,每一个堆内存都有一个引用地址 引用地址指向栈中
//把c赋值给d  实际上就是把c的引用地址复制一份给c 
// b和c的引用地址都指向在栈中同一份地址  所以d变了会改变c的值

Guess you like

Origin blog.csdn.net/weixin_45308405/article/details/127839252