Skip to content
Advertisement

Suppress JavaScript undefined errors?

I have written a script that checks a set of radiobuttons to be checked. But due to different possibilities different radiobuttons will show. Is there a way to suppress JavaScript errors when it pops undefined/getElementById is null? Something like the @-char does in PHP?

Update:

A bit more background info. I’ve made a website where users can submit images and another party for whom the images are can select their top 3 of the images. So each image has three radiobuttons. The difficulty here lies in the fact that the radiobuttons must be controlled dimensional (horizontal and vertical), because a submitted image may only be at place 1, 2 or 3. This is my working code. But adding many if(!var == undefined) doesn’t make the code prettier. Therefor I’m wondering if there is something like @suppressMe is possible?

JavaScript

Advertisement

Answer

You can easily test for a null or undefined value in JavaScript, as both these values are falsy:

JavaScript

You could also consider using a try/catch block:

JavaScript

However be careful that the above will suppress all the TypeError exceptions and that might make your code more difficult to debug.

Advertisement