JavaScript data type six data types (five primitive types, one object type)
- Number
- Sttring
- Boolean
- Null
- Undefined
- Object #对象
The JavaScript data type is a weak data type, and you do not need to specify a data type when defining a variable.
var num = 32;num = "this is a string";32 + 32 // 64 #加法运算//"+"理解为字符串拼接,"-"理解为减法运算"32" + 32 // "3232" # 字符串拼接"32" - 32 // 0 # 减法运算
Implicit conversion skillfully uses "+"/"-" rule conversion type
var num = "string";num - 0 //将num对象转换为numbervar num = 123;num + "" //将num对象转换为string
A = = B #严格等于
- First judgment type, different type, return False
- Same type:
- Number//value same
- string//length and content are the same
- NULL = = NULL
- undefined = = = undefined
- Nan! = Nan//nan is not equal to anything, including comparing yourself.
- New Object! = new Object//is a two empty object and is not equal.
- [1, 2]! = [1, 2]//content is the same, the order is not equal, because they are not exactly the same object
- In JavaScript, the comparison of objects is compared by reference.
A = = B #等于
- Same type, same as = = =
- Type is different, try a type conversion and then compare:
- Null = = undefined//true
- Number = = string//try to turn string number,1 = = "1.0" to True
- Boolean = =? Boolean Turn number,true = 1, false = 0
- Object = = Number | string//Attempt to convert object to basic type new String (' hh ') = = ' Hi '//true
- Other: False
Wrapping Object
var str = "string"; //string类型var strObj = new String("string"); //对象类型,string对应的包装类str.length //str为基本类型,没有属性。当str访问length属性时,javascript会把基本类型转换为对应的包装对象
Type detection typeof
{%note danger%} suitable for detecting basic types and function, encountering null invalidation {%endnote%}
typeof 100 "number"typeof true "boolean"typeof function "function"typeof(undefined) "undefined"typeof new Object() "object"typeof [1, 2] "object"typeof NaN "number"typeof null "object" #返回object而不是null,是由于历史原因
instanceof
The instanceof operator is based on the prototype chain to judge, usage:obj instanceof Object
Note: Object type detection between different window or IFRAME cannot use instanceof!
{%note danger%} can be used to detect custom objects and native objects {%endnote%}
[1, 2] instanceof Array === truenew Object() instanceof Array ===false
Object.prototype.toString.apply ()
{%note%} is suitable for built-in objects and basic types to detect null compatibility issues {%endnote%}
Object.prototype.toString.apply([]); === "[object Array]"Object.prototype.toString.apply(function(){}); === "[object Function]"Object.prototype.toString.apply(null); === "[object Null]"Object.prototype.toString.apply(unfefined); === "[object Undefined]"IE6/7/8 Object.prototype.toString.apply(null); 返回"[object Object]"
The data types of learning notes in the classroom Web JavaScript