A variable can store two types of values: primitive values and reference values.
ES6 introduced a new primitive data type, Symbol, which represents a unique value. It is the seventh data type in the JavaScript language; the first six are: Undefined, Null, Boolean, String, Number, Object.
Primitive Types
There are 6 primitive data types in JavaScript: Undefined, Null, Boolean, Number, String, Symbol (new in ES6)!
Convention:Primitive data types are equivalent to primitive types.
Values of primitive data types are accessed by value.
-
Values of primitive types are immutable.
var str = "123hello321"; str.toUpperCase(); // 123HELLO321 console.log(str); // 123hello321
-
Comparison of primitive types is comparison of their values.
var a = 1; var b = true; console.log(a == b); // true console.log(a === b); // false
Above, a and b have different data types, but they can still be compared by value because an implicit type conversion is automatically performed before the comparison.
== : only compares values
=== : not only compares values, but also compares data types
-
Variables of primitive types are stored in the stack memory (Stack).
var a,b; a = "zyj"; b = a; console.log(a); // zyj console.log(b); // zyj a = "呵呵"; // 改变 a 的值,并不影响 b 的值 console.log(a); // 呵呵 console.log(b); // zyj
Diagram as follows: the stack memory contains the variable identifier and the variable value.
Reference Types
Apart from the 6 primitive data types above, the rest are reference types, collectively called the Object type. In detail, they include: Object type, Array type, Date type, RegExp type, Function type, etc.
Values of reference types are accessed by reference.
-
Values of reference types are mutable.
var obj = {name:"zyj"}; // 创建一个对象 obj.name = "percy"; // 改变 name 属性的值 obj.age = 21; // 添加 age 属性 obj.giveMeAll = function(){ return this.name + " : " + this.age; }; // 添加 giveMeAll 方法 obj.giveMeAll(); -
Comparison of reference types is comparison of references.
var obj1 = {}; // 新建一个空对象 obj1 var obj2 = {}; // 新建一个空对象 obj2 console.log(obj1 == obj2); // false console.log(obj1 === obj2); // falseBecause obj1 and obj2 refer to two different objects stored in the heap memory, the values (reference addresses) of the variables obj1 and obj2 are also different!
-
Values of reference types are objects (Object) stored in the heap memory (Heap).
Unlike other programming languages, JavaScript cannot directly operate on the memory space (heap memory) of objects.var a = {name:"percy"}; var b; b = a; a.name = "zyj"; console.log(b.name); // zyj b.age = 22; console.log(a.age); // 22 var c = { name: "zyj", age: 22 };
Diagram as follows:
The stack memory stores the variable identifier and a pointer to the object in the heap memory.
The heap memory stores the content of the object.
Detecting Types
-
typeofoften used to detect whether a variable is the most basic data type
var a; typeof a; // undefined a = null; typeof a; // object a = true; typeof a; // boolean a = 666; typeof a; // number a = "hello"; typeof a; // string a = Symbol(); typeof a; // symbol a = function(){} typeof a; // function a = []; typeof a; // object a = {}; typeof a; // object a = /aaa/g; typeof a; // object -
instanceofused to determine whether the object pointed to by a constructor's prototype property exists in the prototype chain of another object to be detected
In simple terms, it is to determine whether a variable of a reference type is a specific type of object.
({}) instanceof Object // true ([]) instanceof Array // true (/aa/g) instanceof RegExp // true (function(){}) instanceof Function // true
Original address: https://segmentfault.com/a/1190000006752076