JS check if variable or function exist javascript

Use typeof

// var nMyVariable = 0; // try comment and uncomment this row
 
if(typeof nMyVariable === 'undefined' ){
 alert("Variable not exist");
}ele{
 alert("Variable exist");
}
 
//////////////////////////
// chcek if function exist
function myFunciton (){
  alert("Hello Space!");
}
 
if(typeof myFunction === 'function'){
 myFunction();
}
This entry was posted in Javascript css html and tagged . Bookmark the permalink.


Comments are closed.

627 views