Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this code throw ReferenceError: test is not defined?

var te‌st = 1
console.log(test)

Try running this simple code. It gives error: ReferenceError: test is not defined, despite the fact that I defined that variable. Why is this happening?

like image 566
Michał Perłakowski Avatar asked May 21 '16 14:05

Michał Perłakowski


People also ask

How do you fix ReferenceError is not defined?

Answer: Execute Code after jQuery Library has Loaded The most common reason behind the error "Uncaught ReferenceError: $ is not defined" is executing the jQuery code before the jQuery library file has loaded. Therefore make sure that you're executing the jQuery code only after jQuery library file has finished loading.

How do I fix ReferenceError?

Reference errors in Javascript are mainly thrown when an attempt is made to reference a variable that does not exist or is out of scope. Therefore, in the majority of cases, a ReferenceError can be fixed by making sure that the referenced variable is defined correctly and is being called in the correct scope.

What does it mean when a code is not defined?

An undefined error is when we declare a variable in the code but do not assign a value to it before printing the variable. 9.

What is a ReferenceError in JavaScript?

The ReferenceError object represents an error when a variable that doesn't exist (or hasn't yet been initialized) in the current scope is referenced. ReferenceError is a serializable object, so it can be cloned with structuredClone() or copied between Workers using postMessage() .


1 Answers

In the variable declaration, the variable name contains a zero-width non-joiner (ZWNJ) character (between e and s), which is invisible, because its width is equal to zero. However, ECMAScript specification allows this character as a part of variable name.

However, in the console.log() call, there is just test, without any special characters. Therefore, it throws Reference Error, because the variable name is te<ZWNJ>st, not test.

Fortunately, there's an easy way to check if a variable name contains such characters. You can paste your code into JS Bin or JS Fiddle — they denote these characters with a white dot on a red background. That's how it looks like in JS Fiddle:

I think there are also similar features in some IDEs.

Side note: this is an interesting way to prevent people from copy pasting the code snippets you use in answers into their own code. Consider the following code snippet:

// Warning: non-copy-pastable, it won't work if you copy it into your code.
function a‌dd(a, b) {
  return a + b
}

console.log(a‌dd(2, 3))

There's a ZWNJ character in the function name and the function call, so it works here. However, if someone copied the function into their code and then manually typed console.log(add(3, 4)), it would throw ReferenceError: add is not defined.

Please don't take the above seriously, it's rather a joke than a practical use.

Related

  • What characters are valid for JavaScript variable names?
  • No visible cause for “Unexpected token ILLEGAL”
like image 58
Michał Perłakowski Avatar answered Oct 28 '22 08:10

Michał Perłakowski