0

Trying to retrieve values from undefined in Javascript throws a TypeError exception. I'm reading a book that says you can guard against that error with the && operator. However, it doesn't say why I would want to suppress the error. Can someone please explain why that would be useful?

3
  • 1
    ... You're asking why it would be useful not to have your program die? Do you really need an explanation for why it's bad to have your program fail and wind up in a somewhat unpredictable broken state for your users? Commented Dec 12, 2014 at 18:16
  • I was thinking it'd be bad to to try and access data that doesn't exist and I that I would want to know where that's happening so I can fix it. My experience has been that you catch errors and do something about it, not just plow through them. Commented Dec 12, 2014 at 18:20
  • 1
    There's a lot of situations, when your code has to rely on user's input. If invalid input has been entered, with data-validating you can assign a default value, or ask user for correct information. Commented Dec 12, 2014 at 18:28

1 Answer 1

1

When a TypeError happens, an exception is thrown and that branch of code stops executing (if the exception isn't handled). That is usually an undesirable condition.

So, one can write code that checks to see if an object is valid before trying to access properties on it. There are several different ways to structure that type of code depending upon what you are checking.

For example, if you have an object and you want to read a property on it and you aren't sure if the object will be valid, you can do this:

function speak(obj) {
    if (obj) {
        console.log(obj.greeting);
    }
}

This kind of code will be safe whether obj is valid or not. If you take out the if (obj) check, then this code would throw an exception and stop executing if obj was not a valid object.


If, on the other hand, obj should always be a valid object and you want the program to stop executing with a visible exception if obj is not a valid object, then you can go without the if check and just make sure you test your program sufficiently to make sure obj is always valid before it is used. In the code I write, whether or not I check arguments like this often depends upon whether I am in control of what was passed to the function or whether someone else is in control of that. If I'm in control, then I know whether obj will be valid or whether I need to test for validity. If someone else is in control, then I don't know whether obj will always be valid so it may be desirable to test it.

A middle ground that some libraries choose is to offer a "debug" mode that makes unplanned errors very visible (logging) and a "production" mode that tries to keep going as best as it can without stopping execution. Significantly different behavior in debug mode or production mode can also be a source of problems so one has to be careful with that too.

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

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.