2

I want to check if a variable is undefined and quickly found some stackoverflow answers that said the correct way to do it is to test if(variable==null). But in Chrome I am getting an error saying: Uncaught ReferenceError: xdate is not defined

Huh? The whole reason I am testing is so I don't get errors like this. And I did it just like the approved stackoverflow answers. Here is my code snippet.

        if (xdate == null){
          var dateadd = "";
        } else {
          var dateadd = "&date="+date;
        }
2
  • can you verify if the error is on a different line in your console? Commented Mar 16, 2016 at 14:08
  • Unfortunately, there's a difference between "undefined" and "undeclared" - it looks like in your code it doesn't exist, ie is undeclared. jsfiddle.net/2om07p3d Commented Mar 16, 2016 at 14:18

1 Answer 1

4

use something like this to verify whether variable is undefined or not

if (typeof something === "undefined") {
    alert("something is undefined");
}
Sign up to request clarification or add additional context in comments.

1 Comment

using typeof doesn't cause the error for me. So I am going with this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.