How to check a not defined variable in javascript
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)
原文: http://stackoverflow.com/questions/858181/how-to-check-a-not-defined-variable-in-javascript
How to check a not defined variable in javascript的更多相关文章
- 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没有初 ...
- 图形化界面安装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 ...
- [label][JavaScript][The Defined Guide of JavaScript] 变量的作用域
变量的作用域 一个变量的作用域(scope)是程序中定义这个变量的区域. 全局(global)变量的作用域(scope)是全局性的,即在JavaScript代码中,它处处都有定义. 而在函数之内 ...
- [label][JavaScript][The Defined Guide of JavaScript] 如何声明变量
因为觉得我自己的JavaScript基础很不扎实,或者可以说根本就没有所谓基础,所以就最近一直在看<The Defined Guide of JavaScript> . 在一边看的同时,我 ...
- PowerShell Remove all user defined variable in PowerShell
When PS scripts executes, it is possibly create much user defined variables. So, sometimes these var ...
- oracle check if the display variable is set
- 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 ...
- 2292: Quality of Check Digits 中南多校 暴力枚举
#include <cstdio> #include <algorithm> #include <cstring> #include <iostream> ...
- [label][翻译][JavaScript]如何使用JavaScript操纵radio和check boxes
Radio 和 check boxes是form表单中的一部分,允许用户通过鼠标简单点击就可以选择.当与<textarea>元素的一般JavaScript操纵相比较,这些表单控件(form ...
随机推荐
- BestCoder Round #51 (div.2)
明显是无良心的数学round= = 1000 Zball in Tina Town #include<iostream> #include<cstdio> #include&l ...
- 杂题 UVAoj 10000 Longest Paths
Longest Paths It is a well known fact that some people do not have their social abilities complet ...
- hihoCoder 1389 Sewage Treatment 【二分+网络流+优化】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)
#1389 : Sewage Treatment 时间限制:2000ms 单点时限:2000ms 内存限制:256MB 描述 After years of suffering, people coul ...
- 【动态规划】Vijos P1218 数字游戏(NOIP2003普及组)
题目链接: https://vijos.org/p/1218 题目大意: 一个N个数的环,分成M块,块内的数求和%10,最后每块地值累乘,求最大和最小. n(1≤n≤50)和m(1≤m≤9)太小了可以 ...
- Codeforces Round #326 (Div. 1) - C. Duff in the Army 树上倍增算法
题意:一个n个点的数, m个人住在其中的某些点上, 每个人的标号1-m, 询问u-v 路径上标号前a个人,并输出标号,a < 10. 作法, 利用倍增, ID[j][i] 表示i到i的第2^j个 ...
- JAVA工程师面试总结
HR面: 你为什么要离职?如果是公司层面的,公司为什么要这么做? 你的理想薪资?你觉得你有什么优势胜任你应聘的工作? 你的未来发展规划? BOSS面: 项目的东西,hibernate调优?使用了什么数 ...
- Cyclic Nacklace - HDU 3746(next求循环节)
题目大意:给你一些串,问如果想让这个串里面的循环节至少循环两次,需要添加几个字符(只能在最前面或者最后面添加).比如ababc 需要添加5个就是添加ababc. 分析:其实字符串的长度len-next ...
- Java调用R(三)_系统命令调用
java通过配置的系统命令Rscript直接调用R脚本. 优点:R脚本和Java代码完全分离 缺点:R中变量不能控制 1. Java本地能够成功调用. public void CallR() { Ru ...
- 电商H5制作常使用的排版方式
在很多电商网站或者APP中,经常会出现一些精美夺目的活动宣传海报,吸引着用户点击.购买.如今,电商们可以把海报搬到微信中,做出面向用户群大.传播快的H5制作.那么,制作电商H5制作时可以使用哪三种排版 ...
- maven打一个可执行的jar包
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-depen ...