Original link: 1190000006752076
A variable can hold two types of values, the values of the base type (primitive values) and the value of the reference type (reference values).
ES6 introduces a new primitive data type, Symbol, that represents a unique value. It is the seventh data type of the JavaScript language, the first six kinds are: Undefined, Null, Boolean (Boolean), String (string), number, (object).
Basic Type
JavaScript has 6 basic data types:,,,,, Undefined
Null
Boolean
Number
String
Symbol (new in ES 6)
!
conventions: 基本数据类型
and 原始数据类型
other meanings.
The value of the base data type is accessed by value.
The value of the base type is immutable
var str = "123hello321";str.toUpperCase(); // 123HELLO321console.log(str); // 123hello321
- Comparisons of
-
Base types are comparisons of their values
var a = 1;< Span class= "Hljs-keyword" >var B = true; Console.log (A = = B); //trueconsole.log (a = = B); //false
The data types above A and B are different, but you can also compare values because the of the data type is automatically performed before the comparison, and implicitly conversions
.
-
= =
: Comparison of values only
-
= = =
: Not only is it worth comparing, but also the comparison of data types
li>
The basic type of variable is stored in stack memory (stack).
var a,b;a = "zyj";b = a;console.log(a); // zyjconsole.log(b); // zyja = "呵呵"; // 改变 a 的值,并不影响 b 的值console.log(a); // 呵呵console.log(b); // zyj
The diagram is as follows: The stack memory includes the identifier of the variable and the value of the variable
Reference type
In addition to the above 6 basic data types, the remaining is the reference type, collectively referred to as Object 类型
. Subdivision words, there are:,,, Object 类型
Array 类型
, and Date 类型
RegExp 类型
Function 类型
so on.
The value of a reference type is accessed by reference.
-
< Span class= "Hljs-keyword" >var obj = {name: "Zyj"}; //Create an object obj.name = "Percy"; //Change the value of the Name property Obj.age = 21; //add age Property Obj.givemeall = function (return this.name + " Span class= "hljs-string" > ":" + this.age;}; //Add Givemeall method Obj.givemeall ();
-
Comparison of reference types is the comparison of references
var obj1 = {}; //Create a new empty object Obj1var obj2 = {}; //Create a new empty object Obj2console.log (obj1 = = obj2); //falseconsole.log (obj1 = = = Obj2); //false
because Obj1 and Obj2 refer to 2 different objects stored in heap memory respectively, the value of the variable obj1 and OBJ2 (reference address) is not the same!
The value of a reference type is the object that is stored in the heap memory (heap), unlike other programming languages, where JavaScript cannot directly manipulate the object's memory space (heap memory).
var a = {name:"percy"};var b;b = a;a.name = "zyj";console.log(b.name); // zyjb.age = 22;console.log(a.age); // 22var c = { name: "zyj", age: 22};
The illustrations are as follows:
Detection Type
typeof: Often used to detect whether a variable is the most basic data type
var A;typeof A;Undefineda =Nulltypeof A;Objecta =Truetypeof A;//Booleana = 666; typeof A; //number a = "hello"; //Stringa = symbol (); //Symbola = function (typeof A; //Functiona = []; typeof A; //objecta = {}; typeof A; //objecta = /aaa/g; typeof A; //object
Instanceof: Used to determine whether the object pointed to by the prototype property of a constructor exists on another prototype chain to detect the object
The simple thing is to judge whether a variable of a reference type is specific to a certain type of object
instanceof Object // true([]) instanceof Array // true(/aa/g) instanceof RegExp // true(function(){}) instanceof Function // true
For more detailed type testing, see Resources!
Where there is a mistake, you are welcome to point it out!
References
"article" [JS advanced] Basic type reference type simple Assignment object reference (recommended)
"Stack Overflow" Javascript by reference vs. by value
"article" [[JS] Let the person faint JavaScript variable assignment] (Http://hellobug.github.io/blo ...
How does the article check the JavaScript variable type? Recommended
Three meanings of the "article" stack
JavaScript in-depth understanding of the values of basic types and reference types