检测数据类型

typeof 检测数据类型

 typeof 返回数据类型 number string Boolean function object undefined ;

     返回object

var shuzu = [5,5,3,6,3];
console.log(typeof(shuzu));
//object

 返回number

var Num = 666;
console.log(typeof(Num));
//number

 返回Boolean

var Boo = true,BOo = false;
console.log(typeof(Boo),typeof(BOo));
//Boolean

      返回string

var str = "5555";
console.log(typeof(str));
//string

  返回undefined

console.log(typeof(aaa));
//aaa未定义 所以 返回 undefined

  返回function

var aaa = function(){console.log(555)};
console.log(typeof(aaa))
//返回function

用construtor来检测数据类型 ()

var test=new Array();//改变test 来检测
if (test.constructor==Array)
{
document.write("This is an Array");
}
if (test.constructor==Boolean)
{
document.write("This is a Boolean");
}
if (test.constructor==Date)
{
document.write("This is a Date");
}
if (test.constructor==String)
{
document.write("This is a String");
}
if(test.constructor==Function)
{
document.write("This is a Function");
}

 

posted @ 2019-07-09 15:29  深夜的海洋  阅读(190)  评论(0编辑  收藏  举报