ECMAScript variables contain values of two different data types: base type values and reference type values. Basic data types refer to simple data segments, while reference data types refer to pairs that may have multiple values.
Basic type (value type)
- Undefined is undefined when var is used to declare variables but not initialize them, or when an undeclared variable is used, or when an object attribute does not exist.
- Logically, null values represent an empty object pointer, unique.
- Boolean true,false
- Number NaN, that is, Not a Number, is a special value that is used to represent a case where the operand that is supposed to return the value does not return the value (so that no errors are thrown).
- String strings can be represented by''or'. Strings in ES are immutable, that is, once strings are created, their values cannot be changed. To change the string saved by a variable, first destroy the original string, and then fill the variable with another string containing a new value.
These five basic data types are accessed by value because the actual values stored in variables can be manipulated.
Complex Types (Reference Types)
Object objects are actually a collection of data and functions. Objects can be created by following the new operator with the name of the object type to be created. By creating an instance of Object and adding attributes and methods to it, you can customize the object.
-
- Object type
- Array type
- Date type
- RegExp type
- Function type
The value of the reference type is the object stored in memory. Unlike other languages, js does not allow direct access to the location in memory, that is, it cannot directly manipulate the memory space of the object. When manipulating an object, it is actually manipulating the reference of the object rather than the actual object. (This statement is not very rigorous. When adding attributes to an object, it operates on the actual object).
The difference between value type and reference type
- Basic types occupy a fixed size of space in memory and are therefore stored in stack memory
- Duplicate a basic type of value from one variable to another, duplicating a copy of the value
- The value of the reference type is the object stored in heap memory
- A variable containing a reference type value actually contains not the object itself, but a pointer to the object.
- When the value of a reference type is copied from one variable to another, the copy is a reference pointer, so both variables eventually point to the same object.
Small partners who are interested in Web front-end technology can join our learning circle. They have been working for the sixth year. They can share some learning methods and details of practical development. 767-273-102 autumn skirt. How to learn the front-end from the zero foundation? Look at how our predecessors went ahead in the world of programming! Keep updating the latest teaching and learning methods (web front-end system learning route, detailed front-end project teaching videos), want to learn the front-end of the web, or transfer, or college students, as well as work to enhance their ability, small partners who are learning are welcome to join. We'll go with each other. Front end, front end, front end
Judging data type
Using typeof
The best choice for detecting basic data types is to use typeof
var bool = true var num = 1 var str = 'abc' var und = undefined var nul = null var arr = [1,2,3] var obj = {} var fun = function(){} var reg = new RegExp() console.log(typeof bool); //boolean console.log(typeof num); //number console.log(typeof str); //string console.log(typeof und); //undefined console.log(typeof nul); //object console.log(typeof arr); //object console.log(typeof obj); //object console.log(typeof reg); //object console.log(typeof fun); //function
From the results, we can see that, in addition to returning object when null is detected and returning function when function is detected. Returns all objects for reference types
Use instanceof
The best choice for detecting reference data types is instanceof
console.log(reg instanceof Object); //true console.log(reg instanceof RegExp); //True returns true because the values of the reference types used are all instances of Object as specified. console.log(bool instanceof Boolean); //false var bool2 = new Boolean(); console.log(bool2 instanceof Boolean); //The main difference between the true reference type and the basic wrapper type is the lifetime of the object. Instances of reference types created using the new operator are kept in memory until the execution stream leaves the current scope. Objects of the basic wrapper type created automatically exist only at the moment of execution of a line of code and are immediately destroyed.
console.log(und instanceof Object); // false console.log(nul instanceof Object); // False undefined and null do not have contructor attributes
instanceof detects not only the constructor for constructing an object, but also the prototype chain. So it can detect inherited attributes.
Using constructor
console.log(bool.constructor === Boolean);// true console.log(num.constructor === Number);// true console.log(str.constructor === String);// true console.log(arr.constructor === Array);// true console.log(obj.constructor === Object);// true console.log(fun.constructor === Function);// true web Front-end development learning Q-q-u-n: 767273102 ,Share learning methods and small details that need attention, and keep updating the latest teaching and learning methods (detailed front-end project teaching video)
undefined and null do not have contructor attributes
constructor can't judge undefined and null, and it's not safe to use it, because the direction of the container can be changed // Details please understand the characteristics of js objects
Use Object.prototype.toString.call
console.log(Object.prototype.toString.call(bool));//[object Boolean] console.log(Object.prototype.toString.call(num));//[object Number] console.log(Object.prototype.toString.call(str));//[object String] console.log(Object.prototype.toString.call(und));//[object Undefined] console.log(Object.prototype.toString.call(nul));//[object Null] console.log(Object.prototype.toString.call(arr));//[object Array] console.log(Object.prototype.toString.call(obj));//[object Object] console.log(Object.prototype.toString.call(fun));//[object Function]