Undefined variable

Updated: 04/26/2017 by Computer Hope

An undefined variable is a variable that is used in a program that has not been previously declared in the source code. In most programming languages, this results in an error.

For example, in JavaScript a variable must be declared using the var keyword. If not, trying to assign a value to the variable will result in an error:

X = 1; // causes an error

To fix this, the variable can be defined and then assigned the value:

Var x;
x = 1; // no error

In the case of JavaScript, "undefined" is also a special value that is used for any variable declared with the var keyword but has not yet been assigned a value. To immediately assign a value to a variable, add the assignment to the declaration statement:

Var x = 1; // variable declared and assigned a value

With this in place, the x variable won't have a value of "undefined".

Declaration, JavaScript, Programming terms, Undefined, Variable