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;
        }
Thread7
  • 1,015
  • 2
  • 12
  • 27
  • can you verify if the error is on a different line in your console? – gurvinder372 Mar 16 '16 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. https://jsfiddle.net/2om07p3d/ – freedomn-m Mar 16 '16 at 14:18

1 Answers1

4

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

if (typeof something === "undefined") {
    alert("something is undefined");
}
Pandiyan Cool
  • 5,804
  • 5
  • 45
  • 80