This is similar to a number of other questions on SO, but not exactly the same as any I can find.
Which is the best approach for checking for an undefined value in Javascript, and why?
First example:
var a;
if (typeof(a) === 'undefined'){...}
Second example:
var a;
if (a === undefined){...}
So, the first example is comparing the name of the type to a string, and the second is comparing the variable to the undefined object, using the equality operator, which checks that the types are the same as well as the values.
Which is better? Or are they both as good as one another?
Note that I'm not asking about any difference between undefined and null, or truthy or falsey, just which of these two methods is correct and/or better.
Referencing undeclared variables usually results in a ReferenceError, except when using the typeof keyword. The typeof undefined is the string "undefined" — and undefined is a falsy value that is loosely equal to null but not to other falsy values.
In a JavaScript program, the correct way to check if an object property is undefined is to use the typeof operator. If the value is not defined, typeof returns the 'undefined' string.
It means null is equal to undefined but not identical. When we define a variable to undefined then we are trying to convey that the variable does not exist . When we define a variable to null then we are trying to convey that the variable is empty.
Simply put, undefined means a variable has been declared but has not yet been assigned a value. undefined is a type by itself (undefined). Unassigned variables are initialized by JavaScript with a default value of undefined. //demonstrating usage of undefined in javascript var n; console. log(n); //undefined.
If a variable doesn't exist, then you'll get a reference error when you try to use it — even if you are comparing it to undefined
. So always use typeof
.
> foo === undefined
ReferenceError: foo is not defined
at repl:1:2
at REPLServer.eval (repl.js:80:21)
at Interface.<anonymous> (repl.js:182:12)
at Interface.emit (events.js:67:17)
at Interface._onLine (readline.js:162:10)
at Interface._line (readline.js:426:8)
at Interface._ttyWrite (readline.js:603:14)
at ReadStream.<anonymous> (readline.js:82:12)
at ReadStream.emit (events.js:88:20)
at ReadStream._emitKey (tty.js:320:10)
> typeof foo === "undefined"
true
It is also possible for (bad) code to overwrite undefined
, which would cause an undefined value to not be equal to undefined
.
The undefined
can be assigned a value, and the type check won't work. Unless the scope of the code is protected, e.g.
(function(undefined){
var a;
if (a === undefined) {
})();
// note called without parameter, so undefined is actually an undefined value
this way to check is not safe, and first one is prefered
Edit: It seems that ECMA 5 dissalows assigning value to the undefined, but still this depends on the browser implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With