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. Unity NGUI的多分辨率适配

    参考链接:http://blog.csdn.net/mfc11/article/details/17681429,作者:CSDN mfc11 1.NGUI默认的适配方式: NGUI默认是适配方式是根据 ...

  2. splay模板

    点操作: splay树可以一个一个的插入结点,这样的splay树是有序树,结点权值大于左儿子小于右儿子 这样就是点操作 区间操作: 还有就是可以自己建树,这样的splay树就不是按权值的有序树,它不满 ...

  3. HDU 1269 迷宫城堡 【强联通分量(模版题)】

    知识讲解: 在代码里我们是围绕 low 和 dfn 来进行DFS,所以我们务必明白 low 和 dfn 是干什么的? 有什么用,这样才能掌握他.   1.  dfn[]  遍历到这个点的时间 2.   ...

  4. Android 金融项目整理

    本来是安安心心周末休假的时间,却被强征来加班.魔都今天雾霾严重,窗外都看不到360无死角都能看到的东方明珠.吃完午饭后睡一觉起来,觉得该给自己做点事情了.那就把项目里面的一些可圈可点的技术都罗列出来, ...

  5. 【最短路】FOJ 2243 Daxia like uber

    题目链接: http://acm.fzu.edu.cn/problem.php?pid=2243 题目大意: 给一张N个点M条边的有向图,从s出发,把在x1的人送到y1,在x2的人送到y2用的最短距离 ...

  6. Final对象

    常量指不能改变的量. 在Java中用final标志,声明方式和变量类似: final double PI = 3.1415927; 虽然常量名也可以用小写,但为了便于识别,通常使用大写字母表示常量. ...

  7. HDOJ(HDU) 2148 Score(比较、)

    Problem Description 转眼又到了一年的年末,Lele又一次迎来了期末考试.虽然说每年都要考试,不过今年的这场考试对Lele来说却意义重大. 因为经济原因,如果今年没有排在班级前几名, ...

  8. 高效算法——D 贪心,区间覆盖问题

    Given several segments of line (int the X axis) with coordinates [Li , Ri ]. You are to choose the m ...

  9. WCF—Binding

    原文地址:http://www.cnblogs.com/jams742003/archive/2010/01/13/1646379.html Binding描述了哪些层面的信息 一个Binding包含 ...

  10. 简单的FOLLOW集演示程序

    /* * 该程序用于计算某个非终结符的 FOLLOW 集合 * RexfieldVon * 2013年6月30日16:02:47 */ #include <stdio.h> #includ ...