A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Undeclared_var below:

ReferenceError: assignment to undeclared variable "x" - JavaScript

ReferenceError: assignment to undeclared variable "x"

The JavaScript strict mode-only exception "Assignment to undeclared variable" occurs when the value has been assigned to an undeclared variable.

Message
ReferenceError: x is not defined (V8-based)
ReferenceError: assignment to undeclared variable x (Firefox)
ReferenceError: Can't find variable: x (Safari)
Error type

ReferenceError in strict mode only.

What went wrong?

A value has been assigned to an undeclared variable. In other words, there was an assignment without the var keyword. There are some differences between declared and undeclared variables, which might lead to unexpected results and that's why JavaScript presents an error in strict mode.

Three things to note about declared and undeclared variables:

For more details and examples, see the var reference page.

Errors about undeclared variable assignments occur in strict mode code only. In non-strict code, they are silently ignored.

Examples Invalid cases

In this case, the variable "bar" is an undeclared variable.

function foo() {
  "use strict";
  bar = true;
}
foo(); // ReferenceError: assignment to undeclared variable bar
Valid cases

To make "bar" a declared variable, you can add a let, const, or var keyword in front of it.

function foo() {
  "use strict";
  const bar = true;
}
foo();
See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4