0

Here is the code:

if(!typeOf(node.parentNode)) return null;

Here is the error:

Uncaught TypeError: Cannot read property 'parentNode' of null

I am trying to test if it is null/undefined/false. But it keeps sending me errors.

How can I test it without getting an error with the if statement?

0

4 Answers 4

2

Test the object reference too:

if (!node || !node.parentNode) return null;

If "node" can really be anything (like, a string or a number in addition to an object reference), you'd also want to test the type.

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

Comments

1

As the other answers mentioned, your particular errors come from the fact that your node object is actually null. The most bullet-proof way of testing if node.parentNode exists and is not null is:

if ((typeof node==='undefined') || !node || !node.parentNode) return null;

This covers the following cases:

  • the node variable doesn't exist
  • the node variable is null or undefined
  • parentNode is falsy (undefined, null, false, 0, NaN, or '')

As per Blue Skies' comments, you should take care about the first check (typeof node === 'undefined') because it masks undeclared variables which may lead to problems later on:

function f() {
  if (typeof node==='undefined') {
     node = {}; // global variable node, usually not what you want
  }
}

4 Comments

If the node variable doesn't exist, you should allow the error so that it can be properly declared. The reason is that if it wasn't declared, and there's an assignment somewhere else, it'll create an implicit global. So it's better to get the ReferenceError so you can fix it rather than using typeof to hide it.
@Blue Skies While i generally agree, there are cases where it's useful to have all these checks (ex: while debugging).
I do agree that there are some edge cases, but not in the bulk of one's code. This doesn't help debugging, but rather prevents it because the error is now hidden. Just sayin'... ;-)
I have to pipe in and go with Blue Skies on this one, you are more likely to mask an undeclared variable bug than anything else with that check.
1

You have to check to see if node is null first.

if(!node || !node.parentNode) {
    return null;
}

This is also known as a "short-circuit" evaluation. When it sees that !node is true, it will immediately execute what is inside the block because the operator is an OR (||) and in an OR if one of the inputs is true, then the result can only be true.

Also, typeof is a keyword; not a function (although your code will still work).

4 Comments

I'm not the downvote, but I think checking === null is unnecessary and ill-advised. You should really just check for falsiness of the value to catch undefined as well.
@MikeEdwards OP asked for a null check, but I see your point.
Maybe he has edited, it now says "null/undefined/false" in the question.
@MikeEdwards I see it now!
-1
try {
        if (!typeOf(node.parentNode)) return null;
    } catch (err) {
        console.log(err);
    }

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.