JavaScript: View Source code without Comments

One my clients had a requirement of viewing an object while debugging, and they wanted to view only the source code, without comments.

I first thought of looking out for a similar approach implemented in one of the javascript minifiers. But on further digging, I found that JavaScript has a toSource() method. The drawback is it is gecko-only and works only in Firefox. Since the source code had to viewed by only the developers who were debugging the code, ‘works-only-on-firefox’ was accepted.

Here’s the solution in case you have a similar requirement and are ok with a non-standard solution.

OUTPUT

As you can see, toSource returned only the JavaScript source and not comments. I really wish this was cross-browser. I will probably try my hands on the JSON.stringify method and see if something similar can be achieved.