JavaScript Formatter & Minifier
Beautify, validate, or minify JavaScript code in the browser
JavaScript Formatter & Minifier — what it does
Beautify JavaScript into readable, consistently indented code, minify it down for delivery, or validate it to find out whether it parses at all. Three operations that cover the everyday needs: reading someone else's code, shipping your own, and finding the syntax error that broke the build.
Worked example
A small order-total function pasted as two dense lines with a trailing comment, run through Format.
Input code
function orderTotal(items,taxRate){let sum=0;for(let i=0;i<items.length;i++){sum+=items[i].price*items[i].qty}// add tax
const total=sum*(1+taxRate);return {items:items.length,total:total.toFixed(2)}}
console.log(orderTotal([{price:1250,qty:2},{price:180,qty:10}],0.05));Output — formatted
function orderTotal(items, taxRate) {
let sum=0;
for(let i=0; i<items.length; i++) {
sum+=items[i].price*items[i].qty
}
const total=sum*(1+taxRate);
return {
items:items.length, total:total.toFixed(2)
}
}
console.log(orderTotal([ {
price:1250, qty:2
}, {
price:180, qty:10
}
], 0.05));Format minifies first, so the comment is dropped and no spaces are added around operators (let sum=0). Semicolons inside parentheses do not start a new line, so the for header stays on one line; object literals are opened like code blocks. Minify on the same input returns a single line of 258 characters.
When this tool helps
- Making a minified script readable while debugging a page you did not write.
- Checking a snippet for syntax errors before pasting it into a project.
- Compressing a small script for inline delivery in a static page.
How to use it
- Paste the JavaScript into the input box.
- Choose beautify, minify or validate.
- Review the output or the validation result.
- Copy the formatted or minified code.
Accuracy, limits, and good practice
- Validation catches syntax errors only; code can parse perfectly and still be logically wrong at runtime.
- Minified third-party libraries beautify into readable structure but keep their mangled variable names — expect a, b and c rather than meaningful identifiers.
- For project work, a formatter integrated into your editor keeps style consistent automatically; this tool is for one-off snippets and pasted code.
- Validate before beautifying when a script fails silently: a missing brace is easier to find from an error position than from newly indented code.
Frequently asked questions
Does minifying break my code?
Whitespace removal is safe. Be careful with code that relies on automatic semicolon insertion, which compresses less predictably.
Does it handle modern syntax?
Current JavaScript features are handled as ordinary syntax; very new proposals may not parse in the validator.
Does it change how the code runs?
Not the logic, but the text does change: Format drops comments and rewrites whitespace, and Minify also removes comments. Both preserve behaviour, though code relying on implicit semicolons deserves a test after minifying. Keep the original file if its comments matter.
Can I validate and minify ES module code with import and export?
Yes. A file that starts with import {fmt} from './money.js'; and declares export function label(o){…} shows "Valid JavaScript", and Minify keeps both statements: import{fmt}from'./money.js';export function label(o){…}. Regex literals are recognised too, so .replace(/\//g,'-') survives minifying intact instead of being read as a comment.
Does Format check the code for errors?
Yes. Format runs the same syntax check as Validate before it reformats anything. On const total = items.reduce((a, b) => a + b.price, 0; it prints the browser's message "missing ) after argument list" in the status line instead of "Done." and produces no new output; the output box keeps whatever the previous run left there, so check the status line before copying.
Related tools on this site
- CSS Formatter & Minifier — Format or minify CSS code directly in the browser
- Basic JSON Schema Validator — Validate JSON against common schema rules directly in the browser
- NATO Phonetic Converter — Convert letters and numbers to or from the NATO phonetic alphabet
Further reading: Cleaning Up Text You Pasted From Somewhere Else
Everything above runs inside this page. JavaScript Formatter & Minifier needs no account, no upload, and no server round trip — close the tab and nothing is left behind.
JavaScript 格式化與壓縮能做什麼
把 JavaScript 美化成縮排一致、易讀的程式碼,壓縮成上線用的精簡版,或驗證它是否根本解析得過。三種操作涵蓋日常需求:讀別人的程式、交付自己的程式,以及找出弄壞建置的那個語法錯誤。
實際範例
一個計算訂單總額的小函式,貼成兩行密集程式碼並帶一則行尾註解,按下格式化。
原文:程式碼
function orderTotal(items,taxRate){let sum=0;for(let i=0;i<items.length;i++){sum+=items[i].price*items[i].qty}// add tax
const total=sum*(1+taxRate);return {items:items.length,total:total.toFixed(2)}}
console.log(orderTotal([{price:1250,qty:2},{price:180,qty:10}],0.05));格式化後
function orderTotal(items, taxRate) {
let sum=0;
for(let i=0; i<items.length; i++) {
sum+=items[i].price*items[i].qty
}
const total=sum*(1+taxRate);
return {
items:items.length, total:total.toFixed(2)
}
}
console.log(orderTotal([ {
price:1250, qty:2
}, {
price:180, qty:10
}
], 0.05));格式化會先壓縮,所以註解被丟掉,運算子前後也不加空格(let sum=0)。括號內的分號不會換行,所以 for 的條件部分維持在同一行;物件實字會像程式區塊一樣展開。同一份原文按壓縮會得到單獨一行(258 個字元)。
什麼時候用得上
- 除錯不是自己寫的頁面時,把壓縮腳本變得可讀。
- 把片段貼進專案前,先檢查有無語法錯誤。
- 為靜態頁面的內嵌交付壓縮小型腳本。
使用步驟
- 把 JavaScript 貼進輸入框。
- 選擇美化、壓縮或驗證。
- 檢視輸出或驗證結果。
- 複製整理或壓縮後的程式碼。
精確度、限制與實務建議
- 驗證只抓語法錯誤;程式可以完全解析得過,執行時邏輯仍然是錯的。
- 壓縮過的第三方函式庫美化後結構可讀,但變數名仍是被混淆過的——會看到 a、b、c 而不是有意義的名稱。
- 專案開發請用編輯器內建的格式化工具自動維持風格;本工具適合一次性片段與貼上的程式碼。
- 腳本無聲失敗時請先驗證再美化:從錯誤位置找出漏掉的大括號,比在剛縮排完的程式裡翻找容易得多。
常見問題
壓縮會弄壞程式嗎?
移除空白是安全的。但依賴自動分號插入的程式要小心,壓縮結果較難預測。
支援新語法嗎?
現行 JavaScript 特性都當作一般語法處理;極新的提案語法驗證器可能解析不了。
會改變程式執行結果嗎?
邏輯不會變,但文字會:格式化會拿掉註解並重寫空白,壓縮同樣會移除註解。兩者都保持行為不變,但依賴隱含分號的程式碼壓縮後仍值得測一次。註解有保留價值的話,請留著原始檔。
含 import 與 export 的 ES 模組程式碼可以驗證和壓縮嗎?
可以。開頭是 import {fmt} from './money.js';、並宣告 export function label(o){…} 的檔案會顯示「Valid JavaScript」,壓縮也會保留這兩個陳述式:import{fmt}from'./money.js';export function label(o){…}。正規表達式實字同樣能被辨識,.replace(/\//g,'-') 壓縮後完整保留,不會被當成註解。
格式化會檢查程式碼有沒有錯嗎?
會。格式化在重新排版前會先做與驗證相同的語法檢查。對 const total = items.reduce((a, b) => a + b.price, 0; 按格式化,狀態列會顯示瀏覽器的訊息「missing ) after argument list」,而不是「Done.」,也不會產生新的結果;輸出框仍保留上一次的內容,複製前請先看狀態列。
本站相關工具
- CSS 格式化與壓縮 — 直接在瀏覽器中格式化或壓縮 CSS
- 基礎 JSON Schema 驗證器 — 在瀏覽器中依常用 Schema 規則驗證 JSON
- NATO 拼音字母轉換器 — 將字母與數字轉換為 NATO 拼音字母,或反向解碼
延伸閱讀: 清理從別處貼上的文字
以上步驟全部在這個頁面內完成,JavaScript 格式化與壓縮 不需要註冊、不上傳檔案、也不經過伺服器,關閉分頁後不會留下任何資料。