Uncaught ReferenceError: variable is not defined

← PrevNext →

Your browser console may also sometimes show an error saying, Uncaught ReferenceError: variable is not defined.

See the below image, especially the error message inside the red box.

check if variable is undefined in javascript

let checkVar = () => {
  alert(myName);        // variable myName is not defined anywhere. Therefore it will throw an error.
Try it

Don't confuse this error with the undefined error. Both are different and have different meanings.

In the above example, I have not declared (or defined) the variable myName anywhere in my JS code. It does not know if it’s a string, number or a Boolean. As a result, it throws the error when I am trying to "alert" the variable, expecting to see the value strored in the variable.

← PreviousNext →