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 表示一个未声明的变量,或已声明但没有赋值的变量,或一个并不存在的对象属性
  • 相关阅读:
    特殊字符大全
    ASP執行文件下載
    在 Access 里使用查询建立 存储过程/视图, 并使用 ASP 执行
    通過windows的排定工作來執行存儲過程
    前台页面中用js取得eWebEditor的值
    IE7外觀優化
    电话订票
    在BIOS设置“扩展或板载”显卡的方法
    子窗口中操作父窗口对像(javascript)(转)
    mailto用法
  • 原文地址:https://www.cnblogs.com/fanbi/p/6505938.html
Copyright © 2011-2022 走看看