使用typeof来检查原始类型javascript

时间:2014-07-06 00:29:10

标签: javascript typeof

您好我想使用javascripfor示例检查基本类型

var x //some kind of input is needed like prompt?
if x is a string then alert typeof x is a string

else if x is a boolean alert typeof  x  is a boolean

else 
return alert x is a number 

我不知道如何使用typeof来检查这个因为我刚开始学习javascript。谢谢!

1 个答案:

答案 0 :(得分:0)

typeof "John"                 // Returns string 
typeof 3.14                   // Returns number
typeof false                  // Returns boolean
typeof [1,2,3,4]              // Returns object
typeof {name:'John', age:34}  // Returns object

这里有一些documentation