Why does console.log(z) say 'z is not defined' instead of 'z is undefined'?
Because z was never declared, so it does not exist in any scope. The error message 'not defined' means the variable is missing entirely, which is different from undefined which means the variable exists but has no value.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in undefined vs not defined in JavaScript
undefined is a value assigned to a declared variable that has no value yet. not defined is a ReferenceError thrown when a variable does not exist in any accessible scope.
Use typeof. typeof an undeclared variable returns 'undefined' without throwing a ReferenceError. This is the safe way to check for globals or optional APIs.
Declared but unassigned variables, missing function parameters, functions with no return, accessing a non-existent object property, and the result of void 0.
Still have questions?
Browse all our FAQs or reach out to our support team
