js类型转换

js类型

在了解类型之后,再去了解==与===的区别

==如果之比较“转化成同一类型后的值”看“值”是否相等,===如果类型不同,其结果就是不等。也就是从根本上比较类型。

对于Array,Object等高级类型,==和===是没有区别的进行“指针地址”比较

  • boolean
  • number
  • string
  • object

看下面表格一目了然:

Value Converted to:
String: Number Boolean Object
undefined "undefined" NaN false throws TypeError
null "null" 0 false throws TypeError
true "true" 1   new Boolean(true)
false "false" 0   new Boolean(false)
"" (empty string)   0 false new String("")
"1.2" (nonempty, numeric)   1.2 true new String("1.2")
"one" (nonempty, non-numeric)   NaN true new String("one")
0 "0"   false new Number(0)
-0 "0"   false new Number(-0)
NaN "undefined"   false new Number(NaN)
Infinity "Infinity"   true new Number(Infinity)
-Infinity "-Infinity"   true new Number(-Infinity)
1 (finite, non-zero) "1"   true new Number(1)
{} (any object)   NaN true  
[] (empty array) "" 0 true  
[9] (1 numeric elt) "9" 9 true  
['a'] (any other array) use join() method NaN true  
function(){} (any function "undefined" NaN true  
none
最后修改于:2015年12月22日 13:57