We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS

Наші добровольці ще не переклали статтю цією мовою: Українська. Долучайтеся й допоможіть це зробити!
Ви також можете прочитати цю статтю іншою мовою: English (US).


ReferenceError: "x" is not defined

Error type


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 scope.

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


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 String.prototype.substring() method will work.

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

Wrong scope

A variable needs 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(numbers()); // 5

See also

Мітки документа й учасники

Зробили внесок у цю сторінку: toni-leigh, fscholz, izidima, SphinxKnight, ijazhassan, nmve, foxeshavedens
Востаннє оновлена: toni-leigh,