2 quick Javascript tips

1. Out-of-the-box pretty printing JSON
Have you ever had the need to print out a JSON structure for debugging? Something like:

[code lang=”js”]
console.log("the data is:", JSON.stringify(data));
[/code]

And then the result is a unformatted JSON structure that you copy/paste into your editor for prettyprinting? Well there is a way to avoid that last step. Just pass in two more magic arguments to JSON.stringify and you get prettyprinted JSON right out of the box.

[code lang=”js”]
console.log("the data is:", JSON.stringify(data, null, 2));
[/code]

2. Programmatically set debug breakpoint
If you have minified concatenated Javascript it can sometime be difficult to find the code in Chrome to be able to set a breakpoint at the correct place when you need to debug. The good thing is that you can set the breakpoint in your code with the following statement:

[code]
debugger;
[/code]

Master the React ecosystem!

Get an email everytime I post by signing up to the email list.

Subscribe now and you will get a free code review checklist you can use to improve your own code