ReferenceError: assignment to undeclared variable "x" (Firefox) ReferenceError: "x" is not defined (Chrome) ReferenceError: Variable undefined in strict mode (Edge)
ReferenceError
warning in strict mode only.
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.
In this case, the variable "bar" is an undeclared variable.
function foo() { 'use strict'; bar = true; } foo(); // ReferenceError: assignment to undeclared variable bar
To make "bar" a declared variable, you can add the var
keyword in front of it.
function foo() { 'use strict'; var bar = true; } foo();
© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Undeclared_var