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, there is not a direct equivalent. If you really want to check for specifically for null, do:
Copyif (yourvar === null) // Does not execute if yourvar is `undefined`
If you want to check if a variable exists, that can only be done with try/catch, since typeof will treat an undeclared variable and a variable declared with the value of undefined as equivalent.
But, to check if a variable is declared and is not undefined:
Copyif (yourvar !== undefined) // Any scope
Previously, it was necessary to use the typeof operator to check for undefined safely, because it was possible to reassign undefined just like a variable. The old way looked like this:
Copyif (typeof yourvar !== 'undefined') // Any scope
The issue of undefined being re-assignable was fixed in ECMAScript 5, which was released in 2009. You can now safely use === and !== to test for undefined without using typeof as undefined has been read-only for some time.
If you want to know if a member exists independent but don't care what its value is:
Copyif ('membername' in object) // With inheritance
if (object.hasOwnProperty('membername')) // Without inheritance
If you want to to know whether a variable is truthy:
Copyif (yourvar)
Source
Answer from Natrium on Stack OverflowHi, all, I often do stuff like this in my code, to check for a variable being not null and not undefined.
// check if value is not null and not undefined
if (value) {
...
}
However, I'm now thinking this can leads to bugs, because of 0, "", false and NaN also being falsy.
What is a better way to check a variable is not null and not undefined? I could use this I think, wondering if there is something shorter than this:
if (typeof value !== 'undefined' || value !== null) {
...
}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, there is not a direct equivalent. If you really want to check for specifically for null, do:
Copyif (yourvar === null) // Does not execute if yourvar is `undefined`
If you want to check if a variable exists, that can only be done with try/catch, since typeof will treat an undeclared variable and a variable declared with the value of undefined as equivalent.
But, to check if a variable is declared and is not undefined:
Copyif (yourvar !== undefined) // Any scope
Previously, it was necessary to use the typeof operator to check for undefined safely, because it was possible to reassign undefined just like a variable. The old way looked like this:
Copyif (typeof yourvar !== 'undefined') // Any scope
The issue of undefined being re-assignable was fixed in ECMAScript 5, which was released in 2009. You can now safely use === and !== to test for undefined without using typeof as undefined has been read-only for some time.
If you want to know if a member exists independent but don't care what its value is:
Copyif ('membername' in object) // With inheritance
if (object.hasOwnProperty('membername')) // Without inheritance
If you want to to know whether a variable is truthy:
Copyif (yourvar)
Source
The only way to truly test if a variable is undefined is to do the following. Remember, undefined is an object in JavaScript.
Copyif (typeof someVar === 'undefined') {
// Your variable is undefined
}
Some of the other solutions in this thread will lead you to believe a variable is undefined even though it has been defined (with a value of NULL or 0, for instance).
Videos
There are two things you should understand about undefined:
- the type
undefinedthat can have only one value - the variable
undefined
To explain:
There are so many values of type
number(10, 10.01, 1e1). But there can be only one value of typeundefined, and that value is stored in the variableundefined.That value has no literal representation either. For example, number values
1,100,1e-1are all literals of type number, but the value stored in the variableundefinedhas no literal form.undefinedis a variable, just a normal variable, that JavaScript declares and assigns it the value of typeundefinedin the global scope. So you can do all the following...typeof undefined; // "undefined" undefined = 100; typeof undefined; // "number" undefined = void 0; typeof undefined; // "undefined" window.undefined === undefined; // true window.undefined === void 0; // trueif you don't want to use the variable
undefined, you can generate the value of typeundefinedby the expressionvoid 0-- whose sole purpose is to return a value of typeundefined.
...can anyone please explain to me why this thing has been inserted into JavaScript...
JavaScript has had a history of bad design - not because of the people who designed it but because no one could foresee that this little scripting capability they were adding to Netscape would one day underpin the business of billion dollar companies.
...we have null value...
Although null can do things undefined does, it is more or less related to objects rather than scalars. Indeed, JavaScript considers null itself an object -- typeof null returns "object".
Sorry for answering an older question but the reason you need both undefined and null is simple: in a prototype-based duck-typing language you absolutely must differentiate between "this object does not define a value for X" and "this object says X is nothing/null/empty".
Without this capability there is no way to walk the prototype chain and so inheritance can't work; you must be able to determine that obj.someProp is undefined so you can look at obj.prototype.someProp, and onward up the chain until you find a value. If obj.someProp returned null there would be no way to know if it really was null or just meant "look at my prototype". The only other way around this is to inject some kludgy magic behind the scenes which breaks your ability to fuzz with the prototype chains and do various other bits of JS black magic.
Like much of Javascript, the idea of undefined and null seems wonky and stupid at first, then absolutely brilliant later (then back to wonky and stupid but with a reasonable explanation).
A language like C# will not compile if you access a property that doesn't exist and other dynamic languages often throw exceptions at runtime when you touch a property that doesn't exist, meaning you have to use special constructs to test for them. Plus classes means when an object is instantiated you already know its inheritance chain and all the properties it has - in JS I can modify a prototype 15 steps up the chain and those changes will appear on existing objects.