1

This may be elementary javascript question but I'd like to understand how js works on the inside.

const obj = { a: 'a'};
if(obj)
  console.log('obj is not undefined');
else
  console.log('obj is UNDEFINED');

if(obj.a)
  console.log('obj.a is not undefined');
else
  console.log('obj.a is UNDEFINED');

if(obj.b)
  console.log('obj.b is not undefined');
else
  console.log('obj.b is UNDEFINED');

if(newObj)
  console.log('newObj is not undefined');
else
  console.log('newObj is UNDEFINED');

The output I get when I execute these statements is

obj is not undefined
obj.a is not undefined
obj.b is UNDEFINED
Uncaught ReferenceError: newObj is not defined

Why is newObj throwing ReferenceError and obj.b is not throwing any error?

2
  • Because obj is defined obj.b looks for a property in an already defined object. If the property is not found you get undefined. newObj is not defined in your code and the script has no idea what is newObj, so you get an error. Commented Nov 1, 2017 at 10:20
  • Because the obj in obj.b exists, whereas you never declared a variable for newObj. You would get the same error for obj.b.x. And you will get undefined for window.newObj. Commented Nov 1, 2017 at 10:21

5 Answers 5

1

Because there is no such a thing as newObj. There is no object. The variable itself was never initialized, therefore it does not exists.

The undefined is a special value, which can be assigned to a variable. But when there is no variable defined, then ReferenceError is thrown.

Accessing obj.b means that you are already have an object referenced by variable named obj. This object is basically a hash, and it can have many properties. When property does not exist or its value is undefined, then it return undefined.

See MDN - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined

Sign up to request clarification or add additional context in comments.

1 Comment

This makes more sense. Didn't know undefined is a value that can be assigned to a variable.
1

On if statement, while statement, etc, type value of null, 0(integer of 0), undefined, false(boolean of false), and NaN(not a number) is counted as false,
else counted as true, E.g. integer except 0, true(boolean of true), string, Etc...

So when you executing if (obj){...} the obj return an object value type, which is not one of the 5 value that counted as false, and then will execute the if statement.

When code if (obj.a){...} executed, the obj.a return a string, and then counted as true

when code if(obj.b){...} executed, the obj.b return an undefined value type, and then counted as false and will not execute if statement, but it execute the else statement. And it also occured for executing if(newObj){...}

Comments

0

There is nothing as 'newObj' in memory so it does throw Uncaught ReferenceError.

While we have 'obj' in our memory. So when you ask 'obj.b' then it search for key 'b' in dictionary 'obj'. And so it doesn't have value then it will be undefined.

Thanks

Comments

0

Use

'undefined' === typeof newObj 

to check whether it exists or not, my friend.

Comments

0

It's because You are already defined obj.So checking if obj has b never fire reference error. because obj.b has special value called undefined this is automatically handled by JavaScript. So you can check undefined keys by if(obj.b===undefined). If you want check an object is undefined or not you can use if( typeof newObj==='undefined').

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.