-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){}
0

1 Answer 1

8

Use typeof

if(typeof variable === "undefined"){
  /* It is undefined */
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.