Foreword
- Basic data types: string, number, boolean, undefined, null and Symbol (symbol)
- Reference data types: Object, Function, Date, RegExp, etc.
Characteristics of basic data types
- The basic data types are accessed by value, means that we can operate the actual values stored in a variable;
- Value of the basic data types are immutable, any method can not change the value of a basic type
let name = 'zhangsan' name.substr() console.log(name) // 输出:zhangsan let age = 'firstblood' age.toUpperCase() console.log(age) // 输出:firstblood
substr () and toUpperCase () method returns after a new string with the original defined variable name does not matter.
3. The basic data types are simple assignment assignment (does not affect the value of the original variables)
A 18 = the let the let B = A A ++ the console.log (A) // Output:. 19 the console.log (B) // Output: 18
Reference data types
Reference types are stored in the heap memory object variable is actually a pointer stored in the stack memory (heap memory is stored in the reference address), this pointer to heap memory
var obj1 = new new Object () var obj2 = obj1 obj2.name = 'I have a name' console.log (obj1.name) // I have a name
Data type conversion
Into a string
1, toString () Method: Note, not null and transfer underfined
2, String () method: can turn
ab & = the let 'zhangsan' the let BC = null the let CD = undefined the console.log (ab.toString ()) // Output: zhangsan the console.log (bc.toString ()) // error given console.log (cd.toString ()) // error error console.log (String (ab)) // output: zhangsan console.log (String (bc)) // output: null console.log (String (cd)) // output: undefined
3, implicit conversions: + "" + sides when an operator is a string type, an operator when other types, will be first converted into a string of other types num string concatenation then returns the string
var a = true var str = a + '' console.log('str')
Converted to numeric types
1, Number (): any value can be converted into numeric values, if the string has to be converted is not a character value, return NaN
2, parseInt () / parseFloat (): parseFloat () to convert the string into a floating-point number, parseFloat () and parseInt very similar,
Except that the parseFloat parses the first one. The second encounter. If the end or non-digital content parsing, only integer, resolve to an integer.
var a = '12.3px' console.log(parseInt(a)) // 12 console.log(parseFloat(a)) // 12.3 let b = 'abc2.3' console.log(parseInt(b)) //NAN console.log(parseFloat(b)) //NAN
Converted to a Boolean ()
Apart 0 '' (the empty string) null undefined NaN converted are converted into other false to true
Analyzing the data type JS
1, typeof () function
For primitive data types, we can use typeof () function to determine his data types. But he is not used to distinguish types of reference data, because all references data types will return to "object".
typeof 'seymoe' // 'string' typeof true // 'boolean' typeof 10 // 'number' typeof Symbol() // 'symbol' typeof null // 'object' 无法判定是否为 null typeof undefined // 'undefined' typeof {} // 'object' typeof [] // 'object' typeof (() => {}) // 'function'
2、instanceof
For reference types we use instanceof to determine the type.
var obj = {} obj instanceof Object //true var arr = [] arr instanceof Array //true
3、Object.prototype.toString.call()
Another method provided in javascript advanced programming, a determination can be common to the original data type and reference data types
var arr = [] Object.prototype.toString.call(arr) == '[object Array]' //true var func = function() {} Object.prototype.toString.call(func) == '[object Function]' //true
4、constructor
instanceof and constructor role is very similar. Object detection and instanceof constructor but not the same, and to process the basic data types of detection.
var aa = [1, 2] console.log(aa.constructor === Array) //true console.log(aa.constructor === RegExp) //false console.log((1).constructor === Number) //true var reg = /^$/ console.log(reg.constructor === RegExp) //true console.log(reg.constructor === Object) //false