在 Java 中,您可以在变量上使用instanceOf或getClass()来找出其类型。
如何在非强类型的 JavaScript 中找出变量的类型?
例如,我怎么知道bar是 aBoolean还是 a Number,还是 a String?
function foo(bar) {
    // what do I do here?
}
在 Java 中,您可以在变量上使用instanceOf或getClass()来找出其类型。
如何在非强类型的 JavaScript 中找出变量的类型?
例如,我怎么知道bar是 aBoolean还是 a Number,还是 a String?
function foo(bar) {
    // what do I do here?
}
使用typeof:
> typeof "foo"
"string"
> typeof true
"boolean"
> typeof 42
"number"
所以你可以这样做:
if(typeof bar === 'number') {
   //whatever
}
但是,如果您使用对象包装器定义这些原语(您永远不应该这样做,请尽可能使用文字)时要小心:
> typeof new Boolean(false)
"object"
> typeof new String("foo")
"object"
> typeof new Number(42)
"object"
数组的类型仍然是object。在这里,您确实需要instanceof操作员。
更新:
另一个有趣的方法是检查输出Object.prototype.toString:
> Object.prototype.toString.call([1,2,3])
"[object Array]"
> Object.prototype.toString.call("foo bar")
"[object String]"
> Object.prototype.toString.call(45)
"[object Number]"
> Object.prototype.toString.call(false)
"[object Boolean]"
> Object.prototype.toString.call(new String("foo bar"))
"[object String]"
> Object.prototype.toString.call(null)
"[object Null]"
> Object.prototype.toString.call(/123/)
"[object RegExp]"
> Object.prototype.toString.call(undefined)
"[object Undefined]"
有了它,您就不必区分原始值和对象。
typeof 仅适用于返回“原始”类型,例如数字、布尔值、对象、字符串和符号。您还可以instanceof用于测试对象是否属于特定类型。
function MyObj(prop) {
  this.prop = prop;
}
var obj = new MyObj(10);
console.log(obj instanceof MyObj && obj instanceof Object); // outputs true
使用type:
// Numbers
typeof 37                === 'number';
typeof 3.14              === 'number';
typeof Math.LN2          === 'number';
typeof Infinity          === 'number';
typeof NaN               === 'number'; // Despite being "Not-A-Number"
typeof Number(1)         === 'number'; // but never use this form!
// Strings
typeof ""                === 'string';
typeof "bla"             === 'string';
typeof (typeof 1)        === 'string'; // typeof always return a string
typeof String("abc")     === 'string'; // but never use this form!
// Booleans
typeof true              === 'boolean';
typeof false             === 'boolean';
typeof Boolean(true)     === 'boolean'; // but never use this form!
// Undefined
typeof undefined         === 'undefined';
typeof blabla            === 'undefined'; // an undefined variable
// Objects
typeof {a:1}             === 'object';
typeof [1, 2, 4]         === 'object'; // use Array.isArray or Object.prototype.toString.call to differentiate regular objects from arrays
typeof new Date()        === 'object';
typeof new Boolean(true) === 'object'; // this is confusing. Don't use!
typeof new Number(1)     === 'object'; // this is confusing. Don't use!
typeof new String("abc") === 'object';  // this is confusing. Don't use!
// Functions
typeof function(){}      === 'function';
typeof Math.sin          === 'function';
在 Javascript 中,您可以使用 typeof 函数来做到这一点
console.log(typeof bar);
比其他答案更精确一点(有些人可能会说迂腐):
在 JavaScript 中,变量(和属性)没有类型:值有。此外,只有 6 种类型的值:Undefined、Null、Boolean、String、Number 和 Object。(从技术上讲,还有 7 种“规范类型”,但您不能将这些类型的值存储为对象的属性或变量的值——它们仅在规范本身中使用,以定义语言的工作方式。值您可以明确操作的只有我列出的 6 种类型。)
当它想要谈论“x 的类型”时,规范使用符号“Type(x)”。这只是规范中使用的符号:它不是语言的特性。
正如其他答案所表明的那样,在实践中,您可能想知道的不仅仅是值的类型——尤其是当类型为 Object 时。无论如何,为了完整起见,这里是 Type(x) 的一个简单的 JavaScript 实现,因为它在规范中使用:
function Type(x) { 
    if (x === null) {
        return 'Null';
    }
    switch (typeof x) {
    case 'undefined': return 'Undefined';
    case 'boolean'  : return 'Boolean';
    case 'number'   : return 'Number';
    case 'string'   : return 'String';
    default         : return 'Object';
    }
}