Jump to content

undefined in function


Go to solution Solved by kicken,

Recommended Posts

hi guys

ive been reading about checking for undefined variable checks it seems a good way is to use in window. its actually the only way i could make the check work..anyway, i really need to check foe an undefined variable outside a function's scope but using the in window method is not working. im not returning either false nor true.l

how do i check if a variable [outside a function] is undefined, from within the function?

the variable is passed as an argument.

thanks

Link to comment
https://forums.phpfreaks.com/topic/280961-undefined-in-function/
Share on other sites

It depends on the variable's scope.  If the variable really is in the global scope, you should be able to do this:

if (typeof window.yourVariable=='undefined')  { ... }

or 

if (typeof window['yourVariable']=='undefined')  { ... }
If neither of those are working, then the variable isn't really globally scoped.  Maybe the variable is really a property of some other variable that's globally scoped, e.g. window.someParentVariable.yourVariable or else the variable was defined within some other function or something.

i should mention that the variable im targeting has not been declared...

 

im using

if("myVar" in window) {}

to check whether a variable has been declared, but this doesn't work from inside a function..any ideas why?

function someFn(myVar){
  if("myVar" in window) { // also,      window[myVar] in window      works from within the function, as long as it has been declared. if it hasn't, i get no alert at all.
    alert('ok');
  } else {
    alert('not ok');
  }
}

i don't get any alert. if i haven't declared the variable....

 

any ideas why?

 

thanks

Edited by ricmetal

okay first off, you have "myVar" in quotes in your condition, which means it's looking for that literal variable, not the value you pass to your function.  So unless window.myVar (or window['myVar']) exists, it's going to be false, no matter what you pass to your function. So basically, it looks like you want to pass a value myVar to your function and then check if it exists in the global scope (e.g. - you pass "foobar" and you want to check if the variable foobar exists).  If that is the case, remove the quotes around myVar.

 

 

But 2nd, why are you using the "in" operator in the first place? This isn't the best way to go about it, and can produce some unexpected behavior.  Read up here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in

IOW if you want to pass a value to your function and check if that variable exists, do this:

function someFn(myVar){
 if (typeof window[myVar]=='undefined')  { 
   alert('variable is not defined');
 } else {
   alert('variable is defined');
 }
}

someFn('foobar'); // variable is not defined
var foobar = 'blah'; // now lets define it
someFn('foobar'); // variable is defined

yes, i was using the literal value because thats how mozilla developer page presented it and it works in the global scope check so i  just left it there.

but i realize i had to use the argument passed by the function thus the window[myVar].anyway, removing the quotes still doesnt work. nor checking for 'undefined' with typeof.

from another user, that posted somewhere else, apparently we cannot check if global variables are undefined from within a function.

and the tests ive made show this. at least to my knowledge i havent been able.

 

i've been glancing over the mozilla developer docs, this is where i got the recommended 'best practice' to check for undefined using in

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined

Okay well then you are leaving out some kind of detail then, because yes you most certainly can check if global variables are undefined from within a function, exactly how I showed you in my first post, or else using the in operator as you did. 

 

Perhaps you should elaborate on "it doesn't work".  Can you show your actual code, and how you are calling it? Post your code on jsfiddle.net so that I may see your "not working" code in action.

Okay, so if you look in your js console, you can see that you get an error when you click on the link:  

 

Uncaught ReferenceError: a is not defined

 

This is happening because in your onclick, you are attempting to pass a variable that doesn't currently exist:

 

<a href="#" onclick="newFunc(a);">click</a>

 

IOW newFunc(a) throws an error when it's called because a doesn't exist. 

 

 

And then lets look at your actual function.. I'm still a bit unclear on what you're trying to actually accomplish, but this is wrong:

 

 

if(window[a] in window) {

 

Look back at the link I gave you about in.  It's supposed to be 

 

prop in objectName 

 

but you are doing 

 

objectName[prop] in objectName

 

And whether or not it should be wrapped in quotes depends on what you are actually trying to look for.  Are you trying to check if a exists as a variable, or are you trying to check if the value of a exists as a variable?

 

IOW: let's say I have

 

var a = "foobar";

 

Are you trying to find out if the variable a is defined, or are trying to find out if there is a variable foobar defined?

because i wanted a generic function to check it a variable is undefined.

ive tried and actually i think you cannot pass an undefined (undeclared) variable to a function through an argument.

so i have long left that boat, so to speak. if any workaways to this id like to hear them though

cheers

regards

Edited by ricmetal
  • Solution

You just have to pass the variable as a string, not as a variable. Ie:

newFunc('a');

//not newFunc(a);
Note that trying to create such a generic function is not that useful in any case. It would only work for global variables, not for variables inside a function/closure for example. The code necessary to check if a variable exists is not excessively long either so you do not gain much from wrapping it in a function.

 

Whatever problem you are attempting to solve would most likely be solved better another way, but you'll have to state what your actual problem is that prompted to to want to try and create such a function.

i started out making a function to check for what i think is 'correct data in variables' in PHP

seeing PHP's empty() function returns false when a variable is equal to zero, and i didnt want this.

then i wanted to check a few global configuration variables for an app im building so i decided to port the function to javascript.

that was the whole purpose.

 

anyway, yes passing the variable name seems to do the trick. i dont quite understand the references but ill catch up

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.