2

We're logging javascript errors and one error message that has come up a lot is null is not an object, for example TypeError: null is not an object (evaluating 'foo[bar]'). In my searches about this error and trying to replicate the error, I continually get a Cannot read property 'foo' of null error instead. I cannot figure out a way to do something that results in a null is not an object error.

What is the difference between the two errors and what are some examples for when one would get called instead of the other?

1
  • 1
    AFAIK particular error messages depends on the current JS environment (browser) Commented Jul 21, 2015 at 20:47

3 Answers 3

4

One example is here

When you assign null value to an object and try to access any of it's properties you will get null is not an object.

So, basically if you will try to call any function on an object that has null value will give this error:

  • null is not an object in Safari

  • Cannot read property of null in Chrome

  • TypeError: obj[1] is null in Firefox

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

Comments

0

The reply by hindmost is correct, there are different messages depending on the browser. Given the following code:

var a = null, b = 5;
a[b];

Chrome will give the error Uncaught TypeError: Cannot read property '5' of null while Safari will give the error TypeError: null is not an object (evaluating 'a[b]').

Comments

0

There is not much difference. The message depends on the browser/environment your script is running on.Eg:-

var x = null;
console.log(x.length);

for chrome the message would be

cannot read property 'length' of null

for safari

'null' is not an object (evaluating 'c.length')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.