| Refresh | Home EGTry.com

test the status of a variable. not declared, declared, not initialized, initialized, null


commentsJavascript sourceExecution result in your current browser
undeclared variable
   var isDeclared;
   try {
     v1;
     isDeclared=true;
   } catch (e) {
     isDeclared=false;
   }
  
   document.write("v1 is declared?"+isDeclared);

declared value
   var v1;
   try {
     v1;
     isDeclared=true;
   } catch (e) {
     isDeclared=false;
   }
  
   document.write("v1 is declared?"+isDeclared);

initialized?
    if (v1===undefined) { 
       document.write("v1 not initialed");
     } else {
       document.write("v1 initialed");
    }

initialized
    v1=10;
    if (v1===undefined) { 
       document.write("v1 not initialed");
     } else {
       document.write("v1 initialed");
    }

a null value still an initialized value
    v1=null;
     
    if (v1===undefined) { 
       document.write("v1 not initialed");
     } else {
       document.write("v1 initialed");
    }

uninitialize it
     v1=undefined;
    if (v1===undefined) { 
       document.write("v1 not initialed");
     } else {
       document.write("v1 initialed");
    }

but still declared
   try {
     v1;
     isDeclared=true;
   } catch (e) {
     isDeclared=false;
   }
  
   document.write("v1 is declared?"+isDeclared);