javascript里怎么检查一个未定义的变量?

in JavaScript null is an object. There's another value for things that don't exist, undefined. The DOM returns null for almost all cases where it fails to find some structure in the document, but in JavaScript itself undefined is the value used.

Second, no, they are not directly equivalent. If you really want to check for null, do:

if (null == yourvar) // with casting
if (null === yourvar) // without casting

If you want to check if a variable exist

if (typeof yourvar != 'undefined') // Any scope
if (window['varname'] != undefined) // Global scope
if (window['varname'] != void 0) // Old browsers

If you know the variable exists but don't know if there's any value stored in it:

if (undefined != yourvar)
if (void 0 != yourvar) // for older browsers

If you want to know if a member exists independent of whether it has been assigned a value or not:

if ('membername' in object) // With inheritance
if (object.hasOwnProperty('membername')) // Without inheritance

If you want to to know whether a variable autocasts to true:

if(variablename)

Source

原文: http://stackoverflow.com/questions/858181/how-to-check-a-not-defined-variable-in-javascript

How to check a not defined variable in javascript的更多相关文章

  1. Run-Time Check Failure #3 - The variable 'p' is being used without being initialized.

    Run-Time Check Failure #3 - The variable 'p' is being used without being initialized. 运行时检查失败 变量p没有初 ...

  2. 图形化界面安装oracle报错Could not execute auto check for display colors using command /usr/bin/xdpyinfo. Check if the DISPLAY variable is set.

    问题描述: 在Linux + oracle 安装时,采有root 帐号登录x-windows 界面,然后 $su oracle 登录录安装Oracle 报以下错误: >>> Coul ...

  3. [label][JavaScript][The Defined Guide of JavaScript] 变量的作用域

    变量的作用域 一个变量的作用域(scope)是程序中定义这个变量的区域. 全局(global)变量的作用域(scope)是全局性的,即在JavaScript代码中,它处处都有定义.    而在函数之内 ...

  4. [label][JavaScript][The Defined Guide of JavaScript] 如何声明变量

    因为觉得我自己的JavaScript基础很不扎实,或者可以说根本就没有所谓基础,所以就最近一直在看<The Defined Guide of JavaScript> . 在一边看的同时,我 ...

  5. PowerShell Remove all user defined variable in PowerShell

    When PS scripts executes, it is possibly create much user defined variables. So, sometimes these var ...

  6. oracle check if the display variable is set

  7. What's the difference between using “let” and “var” to declare a variable in JavaScript?

    https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var-to-declare ...

  8. 2292: Quality of Check Digits 中南多校 暴力枚举

    #include <cstdio> #include <algorithm> #include <cstring> #include <iostream> ...

  9. [label][翻译][JavaScript]如何使用JavaScript操纵radio和check boxes

    Radio 和 check boxes是form表单中的一部分,允许用户通过鼠标简单点击就可以选择.当与<textarea>元素的一般JavaScript操纵相比较,这些表单控件(form ...

随机推荐

  1. Primary key and Unique index

    SQL> create table t1(id1 char(2),id2 char(2),id3 char(2)); Table created. SQL> desc t1 Name Nu ...

  2. 【HDOJ】2319 Card Trick

    水题,STL双端队列. /* 2319 */ #include <iostream> #include <cstdio> #include <cstring> #i ...

  3. Linux&shell之Shell脚本

    写在前面:案例.常用.归类.解释说明.(By Jim) 使用多条命令shell脚本的关键是可以输入多条命令,甚至可以将一条命令的结果传递给另一条命令.date;who(两个命令shell脚本的写法) ...

  4. 树状数组(二维):COGS 1532 [IOI2001]移动电话

    1532. [IOI2001]移动电话 ★☆   输入文件:mobilephones.in   输出文件:mobilephones.out   简单对比 时间限制:5 s   内存限制:256 MB ...

  5. SQL中ISNULL的使用

    在敲写相关sql语句时,我们经常会遇到一些空的字符串或者是字段,这就给我们对数据库造成一定的麻烦,系统经常会提示“某值null不能转换”“插入的值不能为空”等等诸如此类的提示,isnull函数会帮助你 ...

  6. [OSX] 取消开机启动

    以Pulse Secure为例 参考:https://kb.pulsesecure.net/articles/Pulse_Secure_Article/KB26679 输入指令: launchctl ...

  7. SRM 406(1-250pt, 1-500pt)

    DIV1 250pt 题意:有几家宠物店,vecort<int>A表示每家宠物店含有小狗占小狗总数的百分比.现在要做扇形统计图统计每家店的小狗百分比,如下图,问作出来的扇形统计图中最多含有 ...

  8. Discuz X2.5 用户名包含被系统屏蔽的字符[解决方法]

    /uc_client/data/cache/badwords.php文件里的内容 删除并用 utf-8格式保存

  9. Android中监听ScrollView滑动停止和滑动到底部

    1.监听ScrollView滑动停止: /********************监听ScrollView滑动停止*****************************/ scrollView.s ...

  10. [Angualr 2] Using FormBuilder

    There are two main functions we’ll use on FormBuilder: • control - creates a new Control• group - cr ...