js中变量类型是由后面的值确定的,以下教大家如何知道当前变量的类型。具体方法如下。
具体步骤
复习一下变量都有哪些类型
JavaScript中变量有number, string, boolean, object, function, undefined
考察什么情况是哪种类型
使用 typeof 属性,可以返回变量的类型
如:
var a = 12;
//alert(typeof a); //number
a = "asdf";
//alert(typeof a); //string
a = true;
//alert(typeof a); //boolean
a = function() {
alert("adfasdf");
};
//alert(typeof a); //function
a = document;
//alert(typeof a); //object
var b;
alert(typeof b); //undefined
变量a在前面赋值,后面的注释就是相应的类型