A JavaScript beautifier takes code that's hard to read and lays it out neatly, with consistent indentation and line breaks. Paste minified or messy code into this JavaScript beautifier and press Beautify. You'll get a clean version in which every statement sits on its own line and each block is properly indented. You can choose 2 or 4 spaces and decide where opening braces go. The tool only changes the whitespace layout of your code, so it behaves in exactly the same way as before.
How to use the JavaScript beautifier
- Paste your code into the box.
- Choose an indent of 2 or 4 spaces.
- Choose a brace style. Same line is the common style in JavaScript.
- Press Beautify.
- Copy the result into your editor.
What beautifying does
Applying stylistic formatting conventions to source code is known as pretty-printing, and the programs that do it are sometimes called code formatters or beautifiers. This tool puts statements on separate lines, indents the contents of blocks, adds spaces around operators and tidies the layout of functions, objects and arrays. The contents of strings are left exactly as they are, and so are your variable names.
A worked example
Take this single line: function f(a){return a+1}f(2). Press Beautify with a 2-space indent, and it becomes four lines. The first is function f(a) {, the second is the indented statement return a + 1, the third is the closing brace, and the last is f(2). Nothing about the way the code runs has changed, but you can now follow it at a glance. With the New line brace style, the opening brace moves down onto a line of its own.
Minified or beautified?
| Minified | Beautified | |
|---|---|---|
| Whitespace | Removed | Added for clarity |
| File size | As small as possible | Larger |
| Best for | Sending to browsers | Reading and editing |
Minification is the process of removing all unnecessary characters from source code without changing its functionality, such as whitespace, newlines and comments. It reduces the amount of data that has to be sent. Beautifying goes in the other direction, but only partly, as the details below explain.
What a beautifier can't bring back
Formatting can restore the layout of code, but it can't restore what was removed for good. If a minifier has renamed a variable from totalPrice to a, then the beautified code will still say a. Comments that were deleted are gone as well. If you need the original names, look for a source map or the project's unminified files. Even so, readable structure makes it far easier to work out what a script is doing.
When a JavaScript beautifier helps
- Debugging: find your way around a minified script.
- Code review: make a messy snippet readable before you discuss it.
- Learning: study how a published script works.
- Cleaning up: tidy code pasted from a forum or an old project.
- Consistency: bring several snippets into a single style.
Choosing a style
Two-space indentation keeps deeply nested code from drifting too far to the right, and it's very common in JavaScript projects. Four spaces make the structure stand out more strongly. Placing the opening brace on the same line is the usual convention in JavaScript, while some developers who are used to other languages prefer it on a new line. Choose one and use it consistently. If you're contributing to an existing project, follow the style that's already there.
Does it check my code?
No. A beautifier arranges your code, but it doesn't run it or verify that it's correct. Invalid code will still be formatted as far as possible, which can look strange. If the output seems wrong, there's probably a missing bracket or quotation mark somewhere in your input, so check that area first. To test your code safely, try it in the online code editor linked below.
Privacy
Your code never leaves your browser. That matters when you're working on private projects or on files that contain keys, although it's still best to remove any secrets before sharing code with anyone else.
A simple workflow for reading minified code
Begin by beautifying the whole file, and then copy it into an editor that lets you search and fold blocks. Look for strings first, such as messages, web addresses or element names, because strings survive minification unchanged and tell you what each part of the script is about. Next, find the outermost functions and work inward, renaming things in your own copy as you begin to understand them. Keep your notes in comments, and keep the beautified file separate from the one that your site really loads. When you've finished investigating, go back to the original source files to make any lasting changes, since edits to a generated file will be lost the next time that the project is built. This approach turns a wall of text into something that you can explore a piece at a time.
Common mistakes
- Expecting original names. Minified names stay minified.
- Beautifying before shipping. Minify the code again for production.
- Mixing styles in a project. Agree on one format.
Related developer tools
Shrink your code with the CSS and JavaScript minifier, try it in the online code editor, and tidy your markup with the HTML formatter.
What next
Use this often? Add JavaScript B to your Home screen.
Frequently asked questions
How does a JavaScript beautifier work?
It reads your code and rewrites the layout with consistent indentation, spaces and line breaks.
Does beautifying change what my code does?
No. It only changes the layout, and strings and names stay the same.
Can it restore minified variable names?
No. Names that a minifier has shortened can't be recovered.
Does it check for errors?
No. It formats code, but it doesn't run or validate it.
Is my code uploaded?
No. Everything happens in your browser.
Was this tool helpful?
What went wrong?
We only receive the reason you pick, never what you typed into the tool.
Thanks for your feedback.