Using Function.toString to Troubleshoot JavaScript Issues

By  on  

Sometimes JavaScript bugs can be the most frustrating damn things in the world. That problem is compounded when a JavaScript file is created from multiple files on the server side or you simply aren't familiar with some of the code used in a project (i.e. troubleshooting an issue with a JavaScript framework you aren't familiar with. Sometimes just identifying the code within a function is enough to point you in the correct direction. So to quickly check the code for a given function, I'll type into the console something like:

myProblemFunction.toString();

That would return something like:

function myProblemFunction() {
	/* bunch of code here */
	
	some.problem().code;
	
	/* bunch of code here */
}

Awesome. Now that I can get the function code at a glance, I can more easily browse through what could be causing the issue and see what needs to be changed. Usually any hint you can get toward finding where the issue resides is a big help!

Recent Features

Incredible Demos

  • By
    HTML5 download Attribute

    I tend to get caught up on the JavaScript side of the HTML5 revolution, and can you blame me?  HTML5 gives us awesome "big" stuff like WebSockets, Web Workers, History, Storage and little helpers like the Element classList collection.  There are, however, smaller features in...

  • By
    WebKit-Specific Style:  -webkit-appearance

    I was recently scoping out the horrid source code of the Google homepage when I noticed the "Google Search" and "I'm Feeling Lucky" buttons had a style definition I hadn't seen before:  -webkit-appearance.  The value assigned to the style was "push-button."  They are buttons so that...

Discussion

    Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!