JavaScript的数据类型对比






<br> function displayInfo(args){<br> var output = &quot;&quot;;<br> if(typeof args.name === &quot;string&quot;){<br> output += &quot;Name: &quot; + args.name + &quot;\n&quot;;<br> }<br> if(typeof args.age === &quot;number&quot;){<br> output += &quot;Age:&quot; + args.age + &quot;\n&quot;;<br> }<br> alert(output);<br> }<br> displayInfo({<br> name: &quot;Nicholas&quot;,<br> age: 29<br> });<br> displayInfo({<br> name: &quot;Greg&quot;<br> })<br>
</body>

这里的typeof args.name === "string"和typeof args.age === "number"的string和number为什么一定要小写以及加双引号呢?

因为类型的名字是字符串,===必须类型相等才能比较
http://www.cnblogs.com/yiki/archive/2012/05/08/2489687.html
http://blog.csdn.net/aa294194253/article/details/42973259

这里是判断类型 类型是字符串,如果就加双银行,js会判断是否定义的变量,没有则会提示未定义,

建议你了解下typeof操作符的定义,对一个值使用typeof操作符的时候会返回以下某个字符串

 “undefined","boolean","string","number","object","function"  

typeof返回的结果本身就是字符串,如果不加引号会被当作对象。===是全等,包括数据类型。
1==‘1’ true
1===‘1’ false