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.