I am trying to create a reusable function that checks if a variable is undefined or not. The strange thing is that it does not work when I pass the variable to the function to execute the code, but if I use the same logic outside of the function, it works. Is there any way to get this function isDefined to work?
//THIS WORKS AND RETURN FALSE
alert(typeof sdfsdfsdfsdf !== 'undefined');
//THIS GIVES AN ERROR, WHY?
//Uncaught ReferenceError: sdfsd is not defined
function isDefined(value) {
alert(typeof value !== 'undefined' && value !== null)
}
isDefined(sdfsd);
?Live example here (check the console for errors): http://jsfiddle.net/JzJHc/
See Question&Answers more detail:os