Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use (typeof(val) === 'undefined') or (val === undefined)?

Tags:

javascript

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.

like image 650
Mick Sear Avatar asked May 28 '12 20:05

Mick Sear


People also ask

What is the value of typeof undefined === typeof null?

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.

How do you know if typeof is undefined?

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.

Is null == undefined?

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.

What is typeof undefined object null undefined not defined?

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.


2 Answers

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.

like image 177
Quentin Avatar answered Sep 18 '22 13:09

Quentin


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.

like image 36
Maxim Krizhanovsky Avatar answered Sep 17 '22 13:09

Maxim Krizhanovsky