How to handle the undefined

Hello, I have been strugling with some variables that are the type of undefined. I’ve looked on internet but I found nothing on how to determine if it’s “undefined” like this :

if (typeof tmp == "undefined") {
    tmp = [0, 0];
    console.log("tmp is undefined");
}

Has anyone as a solution to counter this?

1 Like

you can just do

var tmp;
console.log(tmp === undefined);//true
tmp = 'hmm';
console.log(tmp === undefined);//false
2 Likes

Both undefined & null got no members in them: :hole:


Therefore, we can’t do anything w/ them but check for their presence. :heavy_check_mark:

But b/c both of them are considered “falsy” values we can greatly simplify their check w/ just: :bulb:

if (!tmp) tmp = [0, 0]; or tmp = tmp || [0, 0];. :wink:

Rather than if (tmp == undefined) tmp = [0, 0];. :flushed:

1 Like

Thanks a lot, it worked as expected :slight_smile: