js判断输入是否为空,获得输入的类型

使用typeof算法

typeof的运算数未定义,返回的就是 "undefined".

下面定义x为运算数:

有一下5种情况;

运算数为数字 typeof(x) 返回的就是 "number";

字符串 typeof(x) 返回的就是 "string";

布尔值 typeof(x) 返回的就是 "boolean";

对象、数组 、null  时: typeof(x) 返回的就是 "object";

函数 typeof(x) 返回的就是 "function"。

例如:

<script type="text/javascript">

    var x=123456;

    alert( typeof(x));//弹出“number”。

</script>

原文地址:https://www.cnblogs.com/IT-Monkey/p/3328522.html