--- title: 'ReferenceError: assignment to undeclared variable "x"' slug: Web/JavaScript/Reference/Errors/Undeclared_var translation_of: Web/JavaScript/Reference/Errors/Undeclared_var ---
ReferenceError: assignment to undeclared variable "x" (Firefox) ReferenceError: "x" is not defined (Chrome) ReferenceError: Variable undefined in strict mode (Edge)
엄격 모드(strict mode)에서만 발생하는 {{jsxref("ReferenceError")}} 경고.
선언되지 않은 변수로 값은 할당되었습니다. var
키워드가 없이 할당이 된 것입니다. 선언된 변수와 선언되지 않은 변수 사이에는 차이가 있는데, 이는 예상치 못한 결과를 가져오며, 때문에 JavaScript 엄격모드에서는 에러를 발생시키고 있습니다.
선언된 변수와 선언되지 않은 변수에 대하여 기억해야 할 세 가지:
더 많은 설명과 예제를 필요로 한다면 이 var
참조문서 페이지를 보세요.
선언되지 않은 변수 할당에 대한 에러는 엄격 모드(strict mode code)에서만 발생합니다. 비-엄격 코드에서는 조용히 묵인됩니다.
이런 경우에는, 변수 "bar"는 선언되지 않은 변수가 됩니다.
function foo() { "use strict"; bar = true; } foo(); // ReferenceError: assignment to undeclared variable bar
"bar" 를 선언된 변수로 만들기 위해서, var
키워드를 변수명 앞에 붙여줍니다.
function foo() { "use strict"; var bar = true; } foo();