Undefined variable

Updated: 11/16/2019 by Computer Hope

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

Undefined objects in JavaScript

In JavaScript, a variable must be declared using the var keyword. If not, trying to assign a value to the variable results 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 JavaScript, "Undefined" is a special value that represents a variable that was declared, that a value has not yet been assigned. 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