-2

I am working in php and have a javascript file linked with 2 controllers. Now for one controller I declared a variable that is used by it. But when the second controller goes through it since there is no definition or declaration for it in that controller I get an error that the variable is not defined. I tried using the following statement in javascript to check for its existence or declaration/definition but still got the error. Secondly is this a good practice of using one javascript file for both the controllers? Statements I have tried so far:

if(variable){}

if(variable === null){}
Shahab Ali
  • 389
  • 1
  • 3
  • 23

1 Answers1

7

Use typeof

if(typeof variable === "undefined"){
  /* It is undefined */
}
Eddie
  • 26,593
  • 6
  • 36
  • 58