JavaScript debugging quick tips

some quick tips for debugging JavaScript in a browser (Google Chrome)

To write a message to the browser console:
 console.log('message here');  
 console.info('message here'); //seems to be same as console.log()  

To write a debug-only message to the browser console:
 console.debug('message here'); //does not work in IE8  

To write a warning to the console:
 console.warn('warning message');  

To write an error to the console:
 console.error('warning message');  

To write an object to the browser console:
 console.log(JSON.stringify(object)); //serializes the object to JSON, and writes it out to the console  

Add a breakpoint, forcing the browser to debug at this point:
 debugger; //adds a breakpoint at this line in the JavaScript script.  
note: you can also use this from the console, to pause the execution.

Console only: dump an object to the console:
 dir(object); //writes the entire object to the console, simmilar to a quick watch  


Comments