DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,274 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for Reference Error: JavaScript
Saif Sadiq
Saif Sadiq

Posted on • Originally published at lambdatest.com

Reference Error: JavaScript

How does it feel when you go to give a job interview and after reaching the interview location you find out that the company for which you are here doesn’t even exist.

Obviously you got angry and your mind will start throwing negative thoughts.

Exactly same happens with JavaScript too.

When any value is assigned to undeclared variable or assignment without the var keyword or variable is not in your current scope, it might lead to unexpected results and that’s why JavaScript presents a ReferenceError: assignment to undeclared variable "x" in strict mode. And this error causes a problem in execution of functions.

If you’ve begun to try out JavaScript you might have encountered some pretty baffling errors. I know I sure did…

ReferenceError: assignment to undeclared variable β€œx”

Errors about undeclared variable assignments occur in strict mode code only. In non-strict code, they are silently ignored.
Code without β€˜var’ keyword

function foo() { 
  'use strict'; 
  bar = true; //variable not declared
} 
foo();
Enter fullscreen mode Exit fullscreen mode

What you get after executing above program?? An Error?? πŸ™

How do you need to codeπŸ™‚
Insert β€˜var’ in front of your variable and see your program running

function foo() {
  'use strict';
  var bar = true; //declared variable here
}
foo();
Enter fullscreen mode Exit fullscreen mode

Likewise there are many scripting factors possible to generate reference error in javascript.

ReferenceError: "x" is not defined
ReferenceError: deprecated caller or arguments usage
ReferenceError: can't access lexical declarationX' before initialization
ReferenceError: reference to undefined property "x"
ReferenceError: invalid assignment left-hand side`

Original Source: lambdatest.com

Related Articles

  1. Unknown: Script Error in JavaScript
  2. Uncaught RangeError: Maximum call stack in JavaScript
  3. Debugging JavaScript Using the Browser’s Developer Console

Oldest comments (0)

Classic DEV Post from 2020:

js visualized

πŸš€βš™οΈ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! πŸ₯³

Happy coding!