I have created a function to get type information about a variable. The function works very well except for one case. If I pass an undefined variable the browser does not execute any code and displays error. The following is the function:
function getType(v) {
if (typeof v === 'undefined')
return 'undefined';
else if (v === null) //typeof v will return object if it is null
return 'null';
else if (v instanceof Array) //typeof v will return object if it is an array
return 'array';
else
return typeof v;
}
Example:
getType(thisisundefinedvariable);
Console shows reference error where as according to the code it must return undefined.
EDIT
The browser had gone crazy. Whats the difference between:
getType(thisisundefinedvariable); //This does not work
AND
getType(window.thisisundefinedvariable); //This works