We often talk about the latest scheme for compressing and minimizing our JavaScript. The JavaScript Beautify script aims to do the opposite.

Often, you find a site that is doing something interesting and you want to learn how it works. You check out the source and it is cryptic gibberish. This is where the beautifier comes in to make it a touch more readable.

I really like Aptana for its code formatter – which does a good job at formatting JS code – it doesn’t go as far as other Eclipse-based formatters for beautifying scripting language code in terms of customizable formatting rules, but it still works nicely to help coding well-structured JS source.

I’m with Kae in preferring ternary clauses (and I think most other people would agree, especially in the case of a beautifier which is meant to increase readability). Expanding ternary clauses wouldn’t really be an option for these JavaScript beautifiers since ternary clauses tend to be embedded all over the place and there isn’t an equivalent to a code block such as:

Hmm, problem is that won’t work well for huge scripts. Last time i tried it with a huge one it crashed. There is another one that works good with large scripts and is faster.
It uses to built in toString()-funtions in the browser and is like 4 lines long:>JSTidy
The script, however, has to be valid.

Comment by Olle Lundberg — November 20, 2007

There is a disclaimer though – since it uses the built in function in the browser:

For best results, use Opera, Safari or Konqueror on this page. It partially works in Firefox (nested functions are not tidied, meaning that most scripts you want to tidy still end up unreadable), and fails to reformat code in IE and iCab.