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 表示一个未声明的变量,或已声明但没有赋值的变量,或一个并不存在的对象属性
  • 相关阅读:
    SpringMVC与Hibernate配置备忘
    Intent在Activity间的传值
    高效的JSON处理_ Jackson
    Ireport+JasperReport制作报表三折线图
    SpringMVC与Ibatis整合开发配置备忘
    Ireport+JasperReport制作报表二JasperReport编译模板
    Android开发之简单计算器
    android平台中解析xml文件:SAX、PULL
    SpringMVC SimpleFormController学习备忘
    java的十个大类
  • 原文地址:https://www.cnblogs.com/fanbi/p/6505938.html
Copyright © 2011-2022 走看看