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 ...
随机推荐
- 五种I/O 模式——阻塞(默认IO模式),非阻塞(常用语管道),I/O多路复用(IO多路复用的应用场景),信号I/O,异步I/O
五种I/O 模式——阻塞(默认IO模式),非阻塞(常用语管道),I/O多路复用(IO多路复用的应用场景),信号I/O,异步I/O 五种I/O 模式:[1] 阻塞 I/O ...
- 「Poetize4」上帝造题的七分钟2
描述 Description "第一分钟,X说,要有数列,于是便给定了一个正整数数列.第二分钟,L说,要能修改,于是便有了对一段数中每个数都开平方(下取整)的操作.第三分钟,k说,要能查询, ...
- BZOJ1044: [HAOI2008]木棍分割
1044: [HAOI2008]木棍分割 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1580 Solved: 567[Submit][Statu ...
- qt项目转vs项目
Qt creator是一个非常好用的跨平台项目管理工具和集成开发环境(IDE).但是对于我自己来说Visual Studio依然是我最顺手的开发工具,由于Qt使用了moc,这样要是自己管理Visual ...
- 第一个ios程序
1.ios的理解: Operating System,简称OS,操作系统,ios是苹果操作系统. 2.Xcode开发环境: 苹果公司开发的编程软件,是开发人员建立OS X 和 iOS 应用程序的最快捷 ...
- cf703B Mishka and trip
B. Mishka and trip time limit per test 1 second memory limit per test 256 megabytes input standard ...
- [Audio processing] FFMPEG转音频格式和采样率
利用FFMPEG转音频格式和采样率 import os import string import subprocess as sp #Full path of ffmpeg FFMPEG_BIN = ...
- 《A First Course in Probability》-chaper8-极限定理-各类不等式
詹森不等式: 证明:
- 衬衫面料品牌:Alumo_衬衫_男装_男装:衬衫、法式衬衫、袖扣领带、西服西裤等男士正装服饰-仕族官网
衬衫面料品牌:Alumo_衬衫_男装_男装:衬衫.法式衬衫.袖扣领带.西服西裤等男士正装服饰-仕族官网 衬衫面料品牌:Alumo
- SEDA工作笔记(一)
摘要 在普遍认知中,软件开发实践是一项充满不确定性的工作,这是由于编码工作占据了其绝大部分的工作,而编码本身就是具有极大不确定性的.同样,计算机科学被视作一门门槛低,基于经验,而无理论意义的纯工程类学 ...