How can I compare a non-existing JavaScript object to undefined without getting a Reference Error?

15,511

Solution 1

Use this:

(typeof task === "undefined")

When you use (task === undefined), Javascript needs to find the value of task to see if it is the same as undefined, but it can't look up the name because it doesn't exist, giving you the reference error. typeof is special in that it can safely return the type of a name that doesn't exist.

Solution 2

Addendum to the accepted answer to understand why it doesn't work with some examples you can try yourself in a javascript console.

Comparing directly with undefined type only works if the variable exist. Below is the output you'll get from the Google Chrome browser:

> task === undefined
  ReferenceError: task is not defined

However if the variable exists it will work:

// continued from above
> var task
  undefined
> task === undefined
  true

This is the reason why you should use typeof solution instead because it will work in all cases without throwing errors (and breaking the execution of javascript code).

// continued from above
> typeof notavariable === 'undefined'
  true
> typeof task === 'undefined'
  true

Note that you don't need the typeof check in some cases, such as the properties in a object literal:

// continued from above
> var obj = {}
  undefined
> obj.test === undefined
  true
> obj.test = 1
  1
> obj.test === undefined
  false

This is because properties in an object behave more like values in an associative array:

// continued from above
> obj["test"]
  1
> obj["test"] === undefined
  false

However you can't always be sure this is a case in a function where you have no control over the argument input:

// continued from above
> function TestFunc(arg1) { console.log(arg1) }
  undefined
> TestFunc(notavariable)
  ReferenceError: notavariable is not defined
> TestFunc(task)
  undefined
  undefined
> TestFunc(obj["lol"])
  undefined
  undefined

Hope this exercise helps you out to understand the why's of this comparison.

Share:
15,511
Fawkes5
Author by

Fawkes5

Updated on August 01, 2022

Comments

  • Fawkes5
    Fawkes5 over 1 year

    I want to boolean to come out of this expression

    (task === undefined);
    

    where task is arbitrary and doesn’t appear in the code at all.

    However, when I run this in rhino, I get a reference Error. I WANT TRUE

    Why don’t I get true?

    I want to check if a particular variable has been defined. How do I do it then if this doesn't work?

  • Fawkes5
    Fawkes5 almost 12 years
    Thank you! Why doesn’t JavaScript just declare task undefined, since it is literally undefined?
  • yantrab
    yantrab almost 12 years
    As I mentioned, you can't use the name task if it is undefined, it will raise an exception as you have seen. typeof is special. And Javascript did declare it undefined, it did it by raising the error!
  • nnnnnn
    nnnnnn almost 12 years
    @Fawkes5: there's a difference between "variable exists but has value undefined" and "variable doesn't exist at all".
  • elclanrs
    elclanrs almost 12 years
    Because undefined is an actual value, which is evaluated as false. If you haven't declared the variable first like var foo; then the variable doesn't have an undefined value assigned to it, therefor you have to check if it's type is undefined to really check if it exists. That's why you use quotes around "undefined" because it's a string.
  • Muhammad Umer
    Muhammad Umer over 7 years
    what if reference was declared but never assigned... like var task; this will also be undefined
  • Jake T.
    Jake T. almost 6 years
    Thanks @Spoike, this was as helpful as it was old! Haha.