What is the difference between `UNDEFINED` and `IS NOT DEFINED`

Asked

Viewed 482 times

5

I have an annoying confusion between error messages undefined // (indefinido) and ReferenceError: x is not defined (Erro de Referência: x não está definido).

Chrome error messages.

var a;

a; // undefined (indefinido)
b; // ReferenceError: b is not defined (Erro de Referência: b não está definido)

typeof a; // "undefined" ("indefinido")

typeof b; // "undefined" ("indefinido")

2 answers

4


The difference is that undefined refers to the value of the variable and is not defined is an error message indicating that the variable does not exist in the program that is running.

a; // undefined (indefinido)

The variable is declared in the script, but has no value assigned. No value was set, so its value is rejected.

Notice that this can happen in different ways:

var a;
console.log(a); // undefined

but also:

console.log(a); // undefined
var a;

and this last case is not equal to the problem of b because somewhere in the script the variable is declared. This is no longer possible (and still good) with let or const, then it would be a mistake:

Cannot access 'a' before initialization

b; // ReferenceError: b is not defined

Actually today in modern Javascript (if it didn’t exist var) it would be more correct to say is not declared or is not initialised because it may be declared but have no value assigned.

In this case the variable b is never declared so cannot be used. Even if you were on the bottom line b = 'foo'; That’s not the same as var b = 'foo'; and so the variable is never initialized.

typeof a|b

The case of typeof is different because one of the characteristics of typeof is able to scan the value of the operand even if it is not declared/initialized.

1

undefined is a type of value to represent empty, similar to null. Why Javascript has two types of empty causes discussions, but the idea is that undefined is used as the default value of an uninitialized variable, while null is used in variables that the programmer set manually as empty.

Not definied is an exception thrown when you try to access a variable that has not been declared. It is a simple definition, but a strange behavior, because b should be the equivalent of window.b, and while property window.b can be accessed (having the value undefined), b can only be accessed after being declared or initialized.

What the macro typeof does is basically treat the exception. window.b !== undefined, typeof b !== 'undefined', 'b' in window are all valid ways of checking whether b exists without casting an exception.

  • I’ll disagree a little bit with you :D... "empty" is a definite value. Therefore, "Undefined" is something that has not been defined, so it has no value, not even "empty".

  • 1

    Just to complement, this would be empty var a = ''; and that would be "Undefined": var a;. So emptiness has nothing to do with Undefined.

  • 1

    Difference between "empty" and null/undefined: https://i.stack.Imgur.com/j9vg8.jpg

Browser other questions tagged

You are not signed in. Login or sign up in order to post.