0

I am trying to understand the reasons for the difference in the response to the following:

case a

var x is not declared. So, when I run:

console.log(x)

I get the expected response: Uncaught ReferenceError: x is not defined

case b

let obj = { }
console.log(obj.x)

In this case, undefined gets printed.

Question

In both cases, the binding x is not defined. What explains the difference in the responses ?

Prasad B
  • 27
  • 6
  • 2
    You need to format your question so we can better understand. If you `console.log(x)`, for example, why would _`a`_ be undefined? – Andy Aug 22 '21 at 12:44
  • Its saying case a) and case b) @Andy. Both using x as variable/property name. – The Fool Aug 22 '21 at 12:45
  • 4
    Variables and properties are different things. Different rules. – trincot Aug 22 '21 at 12:45
  • I'm aware of that @TheFool which is why _`a` being undefined is the expected response_ is weird because `a` isn't declared anywhere, and why I wanted the OP to edit the question. – Andy Aug 22 '21 at 12:47
  • Apologies. The response was 'Uncaught ReferenceError: x is not defined' – Prasad B Aug 22 '21 at 12:48
  • Does this answer your question? [What is the difference between null and undefined in JavaScript?](https://stackoverflow.com/questions/5076944/what-is-the-difference-between-null-and-undefined-in-javascript) – Hef Aug 22 '21 at 13:24

3 Answers3

3

Case A: Undefined variables

JavaScript exception "variable is not defined" occurs when there is a non-existent variable referenced somewhere. Forgetting to define a variable before referencing it may be the most common reference error trigger for developers.

let firstName = "xyz"
console.log(lastName); // Uncaught ReferenceError: lastName is not defined

Documentation for ReferenceError: "x" is not defined

Case B: Undefined

JavaScript warning "reference to undefined property" occurs when a script attempted to access an object property that doesn't exist.

let obj = { }
console.log(obj.x)

Undefined Error Documentation - Reference Link

Sanket Shah
  • 2,888
  • 1
  • 11
  • 22
ShaMoh
  • 1,490
  • 3
  • 18
  • 34
1

To answer this question we first need to understand undefined and and console.log method and it's return value.

undefined:

  1. A variable that has not been assigned a value is of type undefined.
  2. A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value.
  3. A function returns undefined if a value was not returned.

console.log : It returns the value of the parameter given.

Your scenario falls under 3rd point, because console.log could not return any value for passed parameter.It returned undefined.

References:Undefined:Developer.Mozilla

console.log:GeeksForGeeks

0

in the first case, you did not declare a variable called x so it will be not declared but if you did it like this:

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

it will not throw any errors. in the second case, there is an existing variable called obj but there is no property called x so it will be undefined and it will not throw any errors except if something depends on it so that is it. I hope my answer helps you.