zoukankan      html  css  js  c++  java
  • 判断Javascript变量是否为空 undefined 或者null(附样例)

    1.变量申明未赋值
    var type; //type 变量未赋值
    1. type==undefined //true
    2. type===undefined //true
    3. typeof(type)=='undefined' //true
    4. typeof(type)==='undefined' //ture
    5. typeof type == 'undefined' //和第3种等价,true
    6. typeof type === 'undefined' //和第4种等价,true
    7. type==null //true
    8. type===null //false
    注: typeof type 的值是"undefined",(typeof 返回的是字符串,有六种可能:"number"、"string"、"boolean"、"object"、"function"、"undefined")。
    2.变量未申明
    1. type1==undefined //Error: Uncaught ReferenceError: type1 is not defined at <anonymous>:1:1
    2. type1===undefined //Error
    3. typeof(type1)=='undefined' //true
    4. typeof(type1)==='undefined' //ture
    5. typeof type1 == 'undefined' //和第3种等价,true
    6. typeof type1 === 'undefined' //和第4种等价,true
    7. type1==null //Error: Uncaught ReferenceError: type1 is not defined at <anonymous>:1:1
    8. type1===null //Error: Uncaught ReferenceError: type1 is not defined at <anonymous>:1:1
    3.JavaScript undefined 属性
    定义和用法
    undefined 属性用于存放 JavaScript 的 undefined 值。
    说明
    无法使用 for/in 循环来枚举 undefined 属性,也不能用 delete 运算符来删除它。
    undefined 不是常量,可以把它设置为其他值。
    当尝试读取不存在的对象属性时也会返回 undefined。
    提示和注释
    提示:只能用 === 运算来测试某个值是否是未定义的,因为 == 运算符认为 undefined 值等价于 null
    注释:null 表示无值,而 undefined 表示一个未声明的变量,或已声明但没有赋值的变量,或一个并不存在的对象属性
  • 相关阅读:
    CentOS 5.5如何通过yum安装Zend Optimizer
    如何更改商品分类名称的长度?
    ECSHOP的后台路径修改
    SVN协同开发时服务端与线上APACHE测试环境网站同步记录 转
    一个简单的替换字符的SQL命令
    svn 的常见操作 import
    SVN制作补丁包的方法
    kloxo密码解决办法
    SVN的分支/标记/合并
    如何将一个表中的所有字母都变成小写
  • 原文地址:https://www.cnblogs.com/fanbi/p/6505938.html
Copyright © 2011-2022 走看看