SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    variable exists or not ...

    Code:
    function exists(variable){ 	
    
    	try{ if ( ?????) throw "exists";
        	 else throw "does not"}
    
    	catch(e){ return e}
    
    }
    var mavar 
    alert(exists('mavar'))
    should be able to say if following existe or not :

    var mavar
    var mavar=''
    var mavar='klhlklkjh'
    var mavar=321321
    //var mavar

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How's this:
    Code:
    function exists(variable){ 	
    
    	try
            {
    		eval(variable);
    	}
    	catch(e)
    	{
    		return "variable does not exist";
    	}
    
    	if(window[variable] == undefined)
    	{
    		return "no value has been assigned to that variable";
    	}
    
    	return "a value was assigned to that variable";
    
    }
    
    var mavar;
    alert(exists('mavar') );
    
    var a = 3123;
    alert(exists('a') );
    
    alert(exists('myStr') );

  3. #3
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great !

    Except for I had to change following line :
    if(window[variable] == null)

    as undefined is undefined

    Thanks !

  4. #4
    SitePoint Addict jtrelfa's Avatar
    Join Date
    Oct 2004
    Location
    Troy, Mi
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd hate to rain on this parade - but why in the world are you writing a function to determine if a variable exists? This seems like a Rube Goldberg way to determine the existence of a variable, doesn't it? Plus it's pretty bad that you're using "eval" within the function.

    What's the overall effect you're trying to achieve? There's probably a better way.

  5. #5
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's just one more of my exporations of javascripts weirdness ...
    I was just wondering how to test if a variable existe or not in the different cases listed above ...

  6. #6
    SitePoint Addict jtrelfa's Avatar
    Join Date
    Oct 2004
    Location
    Troy, Mi
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    typeof(varname) does the same thing, doesn't it?
    Code:
    var me;
    alert(typeof me); //"undefined"
    me = "123456"; 
    alert(typeof me); //"string"
    me = 123456;
    alert(typeof me); //"number"
    alert(typeof undeclaredvar); //"undefined"
    Javascript will often create a variable as "undefined" if you try to access it when it hasn't been initialized.

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,870
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    if (typeof(variablename) != 'undefined') alert('variablename exists');
    if (variablename != null) alert('variablename exists and has a value');
    if (variablename) alert('variablename exists and has a value other than 0 or false');
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  8. #8
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    if (typeof(variablename) != 'undefined') alert('variablename exists');
    if (variablename != null) alert('variablename exists and has a value');
    if (variablename) alert('variablename exists and has a value other than 0 or false')
    That generates a runtime error on the second line if the variable has not been declared.
    Great !

    Except for I had to change following line :
    if(window[variable] == null)
    That doesn't do it either because (undefined==null) is true. First, it might be helpful to determine what we mean when we say a variable exists. All of these variables exist:

    var x;
    var y = undefined;
    var z = null;

    and they all have values assigned to them. The first one is implicitly assigned the value undefined by js. So, if you say a variable exists if it has a value, you can simply write the function like this:
    Code:
    function exists(variable)
    { 	
    
    	try
            {
    		eval(variable);
    	}
    	catch(e)
    	{
    		return "variable does not exist";
    	}
    
    	return "variable exists";
    }
    If you want to try and differentiate between undeclared, declared but undefined, declared but null, and declared with a non-null, non-undefined value assigned to the variable, that requires more work:
    Code:
    function whatValue(variable)
    { 	
    
    	try
            {
    		eval(variable);
    	}
    	catch(e)
    	{
    		return "variable does not exist";
    	}
    
    	if(window[variable] === undefined)
    	{
    		return "undefined was either explicitly assigned to the variable, e.g. \n\nvar varName = undefined;\n\n or undefined was implicitly assigned to the variable, e.g. \n\nvar varName;"
    	}
    	
    	if(window[variable] == null)
    	{
    		return "null was assigned to the variable";
    	}
    
    	return "a non-null, non-undefined value was assigned to the variable";
    }
    I can't figure out a way to distinguish between:
    Code:
    var x;
    var y = undefined;
    Plus it's pretty bad that you're using "eval" within the function.
    In most scripts you see, that is undoubtedly true; but occasionally eval() can be useful.
    Last edited by 7stud; Feb 7, 2007 at 16:49.

  9. #9
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    weird ..
    I'm still fighting with undefined = undefined ...

  10. #10
    SitePoint Addict jtrelfa's Avatar
    Join Date
    Oct 2004
    Location
    Troy, Mi
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SpaceFrog View Post
    weird ..
    I'm still fighting with undefined = undefined ...
    undefined (no quotes) is a variable in the global scope. i.e. These two statements are identical:

    var x;
    var x = undefined;

    This statement is different:

    var x = "undefined";

    Because "undefined" is actually a string, not the global undefined variable.

    Also, there is a difference between using the strict comparison operator === and the 'regular' comparison == operator:

    var x;

    x === undefined <- only checks to see if x is undefined
    x == undefined <- checks to see if x is undefined and also checks to see if x is null

  11. #11
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    welle even when I test
    x===undefined
    I have a return :
    undefined is undefined ... looks like undefined is not undrestood by Javascript ...
    Is there a minimum required version ?

  12. #12
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    weird ..
    I'm still fighting with undefined = undefined ...
    welle even when I test
    x===undefined
    I have a return :
    undefined is undefined ...
    Do you really expect those statements to be understood by anyone?

  13. #13
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,870
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Anything except for testing if the type of the variable is undefined will actually create the variable in order to test it.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •