ITPub博客

首页 > Linux操作系统 > Linux操作系统 > javascript中的typeof 的用法详解

javascript中的typeof 的用法详解

原创 Linux操作系统 作者:bhdxyjg 时间:2008-08-13 09:44:54 0 删除 编辑

经常会在 js 里用到数组,比如 多个名字相同的 input,若是动态生成的,提交时就需要判断其是否是数组。

if(document.mylist.length != "undefined" ) {} 这个用法有误.

正确的是 if( typeof(document.mylist.length) != "undefined" ) {}

或 if( !isNaN(document.mylist.length) ) {}

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

运算数为数字 typeof(x) = "number"

字符串 typeof(x) = "string"

布尔值 typeof(x) = "boolean"

对象,数组和null typeof(x) = "object"

函数 typeof(x) = "function"

typeof 运算符返回一个用来表示表达式的数据类型的字符串。
可能的字符串有:"number"、"string"、"boolean"、"object"、"function" 和 "undefined"。
如:
alert(typeof (123));//typeof(123)返回"number"
alert(typeof ("123"));//typeof("123")返回"string"

typeof 运算符

返回一个用来表示表达式的数据类型的字符串。

typeof [ ( ] expression [ ) ] ;

expression 参数是需要查找类型信息的任意表达式。

说明

typeof 运算符把类型信息当作字符串返回。typeof 返回值有六种可能: "number," "string," "boolean," "object," "function," 和 "undefined."

typeof 语法中的圆括号是可选项。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/14755392/viewspace-425598/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: JavaScript通用库
请登录后发表评论 登录
全部评论

注册时间:2008-08-13

  • 博文量
    7
  • 访问量
    9338