10

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/

1
  • You can't do this. How can you get it into your function to test it if it doesn't exist? Also this: typeof value !== 'undefined' && value !== null is terribly unnecessary. If you want a null or undefined test, just do value == null. It accomplishes the same thing. Commented Aug 9, 2012 at 0:08

1 Answer 1

9

You cannot use a variable that hasn't been declared unless it's in a typeof test

When you try to pass a variable that hasn't been declared into a function, that is considered using that undeclared variable. You'll notice that the error is in the caller, not inside isDefined

You need to run the check for

if (typeof sdsdsd !== 'undefined')

before you pass it into the function. Basically that means you can't write a isDefined function that accepts undeclared variables. Your function can only work for undefined properties (which are OK to pass around)

However, I am curious, what is the real world case where you're passing a variable that doesn't exist? You should declare all your variables and they should exist already. Had you declared var sdsdsds it would exist with the value of undefined and your isDefined function would work just fine.

Sign up to request clarification or add additional context in comments.

5 Comments

Indeed, your last paragraph made me think to re-architect things more elegantly. My scenario is that a variable is being rendered through an MVC view. But the view has a condition to not render it if there is no results to the request. So my initialize Javascript (from another view), checks if that object exists or not to perform an action. So you are right, I should declare the variable outside of the MVC condition. Thanks for the advice!
Another case that you can use a variable that is not declared is delete.
Given that you can assign values to undeclared variables, I'm pretty surprised this doesn't work. Gonna have to re-do some code that has a method almost identical to the one in the question in it.
@DCShannon In strict mode, you cannot assign values to undeclared variables, JavaScript has changed its mind and you should be using use strict in all your code so you cannot set an undeclared variable
@JuanMendes I would probably do that if I were starting a new project from scratch, but I have legacy code to work with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.