If (typeof s== "undefined") vs if (s == undefined)... both do exactly the same thing?

I do see

if (typeof s == “undefined”) { … }

and sometimes

if (s == undefined) { … }

do both do exactly the same thing? if they are the same, then maybe the second one is more preferred, since it is shorter and no need to quote the word “undefined”.

I am doing this to check for optional parameters for a function… so that if a param is undefined, then it will use some default value for it. Thanks.

Old browsers used to say undefined== any falsy value, so you usually see the === syntax in these tests.

Both s== undefined and s=== undefined comparisons will throw an error if s is an undeclared variable,
while it is safe to use typeof s in that case.

For the purpose you are using these for - the variable will be declared in your function signature - so you don’t need to worry about the undeclared case which mrhoo correctly references.

Beyond that you are fine with either way. As a guide… Resig and the guys at jQuery use both methods quite regularly in the jQuery source code… which is some excellent source, so I consider both to be pretty acceptable (just == not ===).

Commonly the or operator is used to provide default values.

Example:


function someEventHandler(evt) {
    evt = evt || window.event;
    var targ = evt.target || evt.srcElement;
}

which is similar to doing:


function someEventHandler(evt) {
    if (evt == undefined) {
        evt = window.event;
    }
    var targ = evt.target;
    if (targ == undefined) {
        targ = evt.srcElement;
    }
}

what if it is a generic function like

function foo(s, opt) {

}

then

opt = opt || -1

won’t work if the caller indeed pass in a 0 or null?

Correct. If you know the values might be falsy, then checking against undefined is the better course of action.

so

if (typeof s == “undefined”)

is the most bulletproof and universally compatible? (won’t cause exception or error).

i checked a few javascript books and this method and optional param are not mentioned…

While both forms are valid, I prefer typeof(s) == “undefined”, rather than typeof s == “undefined”

I am rather surprised that even in Javascript the Definitive Guide 5th Ed, it is recommended

Section 8.2.1. Optional Arguments

function copyPropertyNamesToArray(o, /* optional */ a) {
if (!a) a = ; // If undefined or null, use a blank array
for(var property in o) a.push(property);
return a;
}

With the function defined this way, you have flexibility in how it is invoked:

// Get property names of objects o and p
var a = copyPropertyNamesToArray(o); // Get o’s properties into a new array
copyPropertyNamesToArray(p,a); // append p’s properties to that array

Instead of using an if statement in the first line of this function, you can use the || operator in this idiomatic way:

a = a || ;

so this method won’t work if the param passed in can be falsy like 0 or null. the solution doesn’t address the need for passing in a number, in which case the number can be 0.

Oh it will work all right, it’s just that if you won’t be able to stop 0 or null being set to the default value.

so let me try to summarize undefined:

[thanks to Paul’s hint down below, the == is changed to === for the comparison with undefined]

  1. Javascript has 5 basic data types, and one of them is Undefined. (the 5 basic types are Number, String, Boolean, Null, and Undefined)

  2. the only possible value of this type is undefined. undefined is a value, just like 1, 1.23, 0, NaN, “hello”, true, null

  3. either when a variable is undeclared, or when the variable is declared but not assigned any value (such as var s; or just function foo(c, s) and the caller doesn’t pass in a 2nd argument), then typeof s will be a string “undefined”

  4. if a variable is declared, then checking it with s === undefined is fine, but if the variable is undeclared, then checking it with s === undefined will cause a Javascript error

  5. when a function takes in 2 params and the caller of the function doesn’t pass in a 2nd argument, it is fine to compare that param using (s === undefined), no need to use (typeof s == “undefined”)

  6. so inside a function, to check whether a param is passed in, we can use if (s === undefined) s = some_default_value;

  7. in other places, the safest way to check whether something is undefined or undeclared is to use (typeof s == “undefined”)

  8. note that (s === undefined) has no quotes around the word undefined. and (typeof s == “undefined”) has quotes around the word undefined.

That’s a good summary. Here’s another summary of the undefined value from the Mozilla Development Center. Their [URL=“http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference”]javascript reference and [URL=“http://developer.mozilla.org/en/Core_JavaScript_1.5_Guide”]javascript guide have always been a good source of information for me.