Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1063612 of ReferenceError: "x" is not defined

  • Revision slug: Web/JavaScript/Reference/Errors/Not_defined
  • Revision title: ReferenceError: "x" is not defined
  • Revision id: 1063612
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment new page

Revision Content

{{jsSidebar("Errors")}}

Message

ReferenceError: "x" is not defined

Error type

{{jsxref("ReferenceError")}}.

What went wrong?

There is a non-existent variable referenced somewhere. This variable needs to be declared, or you need make sure it is available in your current script or {{Glossary("scope")}}.

When loading a library (such as jQuery), make sure it is loaded before you access library variables, such as "$". Put the {{HTMLElement("script")}} tag that loads the library before your code that uses it.

Examples

Variable not declared

foo.substring(1); // ReferenceError: foo is not defined

The "foo" variable isn't defined anywhere. It needs to be some string, so that the {{jsxref("String.prototype.substring()")}} method will work.

var foo = "bar";
foo.substring(1); // "ar"

Wrong scope

A variable need to be available in the current context of execution. Variables defined inside a function cannot be accessed from anywhere outside the function, because the variable is defined only in the scope of the function

function numbers () { 
  var num1 = 2, 
      num2 = 3;
  return num1 + num2;
}

console.log(num1); // ReferenceError num1 is not defined.

However, a function can access all variables and functions defined inside the scope in which it is defined. In other words, a function defined in the global scope can access all variables defined in the global scope.

var num1 = 2,
    num2 = 3;

function numbers () {
  return num1 + num2; 
}

console.log(num1); // 2

See also

  • {{Glossary("scope")}}

Revision Source

<div>{{jsSidebar("Errors")}}</div>

<h2 id="Message">Message</h2>

<pre class="syntaxbox">
ReferenceError: "x" is not defined
</pre>

<h2 id="Error_type">Error type</h2>

<p>{{jsxref("ReferenceError")}}.</p>

<h2 id="What_went_wrong">What went wrong?</h2>

<p>There is a non-existent variable referenced somewhere. This variable needs to be declared, or you need make sure it is available in your current script or {{Glossary("scope")}}.</p>

<p>When loading a library (such as jQuery), make sure it is loaded before you access library variables, such as "$". Put the {{HTMLElement("script")}} tag that loads the library before your code that uses it.</p>

<h2 id="Examples">Examples</h2>

<h3 id="Invalid_cases">Variable not declared</h3>

<pre class="brush: js example-bad">
foo.substring(1); // ReferenceError: foo is not defined
</pre>

<p>The "foo" variable isn't defined anywhere. It needs to be some string, so that the {{jsxref("String.prototype.substring()")}} method will work.</p>

<pre class="brush: js">
var foo = "bar";
foo.substring(1); // "ar"</pre>

<h3>Wrong scope</h3>

<p>A variable need to be available in the current context of execution. Variables defined inside a function cannot be accessed from anywhere outside the function, because the variable is defined only in the scope of the function</p>

<pre class="brush: js example-bad">
function numbers () { 
  var num1 = 2, 
      num2 = 3;
  return num1 + num2;
}

console.log(num1); // ReferenceError num1 is not defined.</pre>

<p>However, a function can access all variables and functions defined inside the scope in which it is defined. In other words, a function defined in the global scope can access all variables defined in the global scope.</p>

<pre class="brush: js">
var num1 = 2,
    num2 = 3;

function numbers () {
  return num1 + num2; 
}

console.log(num1); // 2</pre>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{Glossary("scope")}}</li>
</ul>
Revert to this revision