Undefined variable


title: "Undefined variable" type: doc version: 1 created: 2026-02-28 author: "Wikipedia contributors" status: active scope: public tags: ["variable-(computer-science)", "software-bugs"] topic_path: "technology/computing" source: "https://en.wikipedia.org/wiki/Undefined_variable" license: "CC BY-SA 4.0" wikipedia_page_id: 0 wikipedia_revision_id: 0

An undefined variable in the source code of a computer program is a variable that is accessed in the code but has not been declared by that code.

In some programming languages, an implicit declaration is provided the first time such a variable is encountered at compile time. In other languages such a usage is considered to be sufficiently serious that a diagnostic being issued and the compilation fails.

Some language definitions initially used the implicit declaration behavior and as they matured provided an option to disable it (e.g. Perl's "use warnings" or Visual Basic's "Option Explicit").

Examples

The following provides some examples of how various programming language implementations respond to undefined variables. Each code snippet is followed by an error message (if any).

[[CLISP]]

::code[lang=lisp] (setf y x) ::

- - EVAL: variable X has no value

[[C (programming language)|C]]

::code[lang=c] int main() { int y = x; return 0; } ::

foo.c: In function main': foo.c:2: error: x' undeclared (first use in this function) foo.c:2: error: (Each undeclared identifier is reported only once foo.c:2: error: for each function it appears in.)

[[JavaScript]]

A ReferenceError only happens if the same piece of executed code has a or a (but not ) declaration later on, or if the code is executed in strict mode. In all other cases, the variable will have the special value .

::code[lang=javascript] "use strict"; let y = x; ::

::code[lang=javascript] let y = x; let x; // causes error on line 1 ::

ReferenceError: x is not defined Source File: file:///c:/temp/foo.js

[[Lua (programming language)|Lua]]

::code[lang=lua] y = x ::

(no error, continuing) ::code[lang=lua] print(y) ::

nil

[[Standard ML|ML]] (Standard ML of New Jersey)

::code[lang=sml] val y = x; ::

stdIn:1.9 Error: unbound variable or constructor: x

[[MUMPS]]

Set Y=X

[[OCaml]]

::code[lang=ocaml] let y = x;; ::

Unbound value x

[[Perl]]

::code[lang=perl] my $y = ($x // 0) + 1; # defined-or operator ::

(no error)

[[PHP]] 5

::code[lang=php] $y = $x; ::

(no error)

::code[lang=php] $y=""; $x=""; error_reporting(E_ALL); $y = $x; ::

PHP Notice: Undefined variable: x in foo.php on line 3

[[Python (programming language)|Python]]

Python 3

::code[lang=python3] x = y Traceback (most recent call last): File "<pyshell#0>", line 1, in x = y NameError: name 'y' is not defined ::

Python 2.4

::code[lang=pycon]

x = y Traceback (most recent call last): File "", line 1, in NameError: name 'y' is not defined ::

[[REXX]]

::code[lang=rexx] signal on novalue y = x ::

+++ Error 30 in line 2: Label not found

[[Ruby (programming language)|Ruby]]

::code[lang=irb] irb(main):001:0> y = x NameError: undefined local variable or method `x' for main:Object from (irb):1 ::

[[Tcl (programming language)|Tcl]]

::code[lang=tcl] % set y $x can't read "x": no such variable ::

[[VBScript]]

::code[lang=vbscript] Dim y y = x ::

(no error)

::code[lang=vbscript] Option Explicit

Dim y y = x ::

(3, 1) Microsoft VBScript runtime error: Variable is undefined: 'x'

References

References

  1. "undefined variable." YourDictionary, n.d. Web. 24 July 2013. <http://computer.yourdictionary.com/undefined-variable {{Webarchive. link. (2013-04-15 >.)

::callout[type=info title="Wikipedia Source"] This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page. ::

variable-(computer-science)software-bugs