5

For a variable x, let typeof x === 'undefined'

I have written a function which checks whether variable is undefined or not

var isUndefined = function(obj){
  return typeof obj == 'undefined';
};

Here are two scenarios :

  1. console.log(typeof x === 'undefined') // returns true
  2. isUndefined(x) // throws this error ReferenceError: x is not defined

Why I cant pass an undefined variable here?

Aman Gupta
  • 3,627
  • 4
  • 40
  • 64
  • 1
    You can only “examine” the type of undeclared variables directly with the `typeof` operator. You can’t pass undeclared variables. – Sebastian Simon Oct 10 '16 at 07:21
  • 2
    if you need this kind of work around, you may better use `'use strict';` and declare all variables. the only use is actually the check of a property which is not set. – Nina Scholz Oct 10 '16 at 07:23
  • refer Undeclared vs Undefined – Rajesh Oct 10 '16 at 07:24

3 Answers3

4

See this post. You have to understand the difference between undefined and undeclared variables.

At least you would do:

var x; //undefined until assignment;
isUndefined(x); // returns true;
Community
  • 1
  • 1
bash0ne
  • 394
  • 3
  • 14
1

Basically, undefined represents value, which are not defined formally by user. Consider your example :

var isUndefined = function(obj){
  return typeof obj == 'undefined';
};

So, isUndefined() when invoked without any argument, then the value of obj will be undefined in the function.

Mangu Singh Rajpurohit
  • 10,806
  • 4
  • 68
  • 97
1

Variable x is not declared but you are asking typeof in global context

typeof x === 'undefined' // Returns true

Which is equivalent to below

typeof window.x === 'undefined' //True

But the below code

x === undefined //Throws error

And

x in window //Throws Error

as the variable is never set or declared in the global scope with any standard JavaScript datatypes.

Read MDN blog, Which states that :

JavaScript is a statically scoped language, so knowing if a variable is declared can be read by seeing whether it is declared in an enclosing context. The only exception is the global scope, but the global scope is bound to the global object, so checking the existence of a variable in the global context can be done by checking the existence of a property on the global object

Now when calling the isUndefined function you will be passing the reference of the variable x which is not declared in the parent scope and runs the code in functional scope trying to take the reference of x.

As x is no where declared, there is no meaning or value to be passed by javascript interpreter at runtime to isUndefined. Hence throws the Reference error asking the develeper to atleast declare the variable which when not assigned any value will be set default to primitive type undefined.

Now by setting x to primitive type undefined explicitly as below

var x; // Equivalent to window.x

or

var x = undefined; //Equivalent to window.x = undefined

Now the JavaScript interpreter is aware that there a variable called x declared in global scope window and is set to primitive datatype. Below statements become true:

typeof x === 'undefined' // Returns true
x === undefined //Returns true
x in window // Returns true
isUndefined(x) // Returns true
Sylvain Rodrigue
  • 4,751
  • 5
  • 53
  • 67
NiRUS
  • 3,901
  • 2
  • 24
  • 50