JS Minifier
Paste JavaScript to remove comments and unneeded whitespace. Line breaks the code relies on and /*! license comments stay; variable names are not shortened. How it works
Use the JS Minifier
452 characters before, 269 after: 40.5% saved.
Removes comments and the whitespace JavaScript does not need. Strings, template literals and regular expressions are copied exactly. A line break is kept where the code relies on automatic semicolon insertion (a statement written without a semicolon, or return followed by a line break), so the program still means the same thing. Names are not shortened and nothing is rewritten, and the code is not checked: invalid JavaScript stays invalid. A regular expression written right after a ) that does not close the head of an if, for, while or with, or right after }, is read as a division, and a run of spaces inside it is cut to one. Input up to 2,000,000 characters; above 200,000 it runs in a background worker. Your code is minified in this browser tab and is not sent to a server.
About the JS Minifier
The JS minifier removes comments and the whitespace JavaScript does not need, and changes nothing else: the example script it opens with goes from 452 to 269 characters, 40.5% saved. Strings, template literals and regular expressions are copied exactly, the line breaks that automatic semicolon insertion relies on are kept, and variable names are not shortened. Paste code or open a .js, .mjs, .cjs or .jsx file.
Before and after
| Input | Output |
|---|---|
| let a = 1 ⏎ let b = 2 | let a=1 ⏎ let b=2 |
| let a = 1; ⏎ let b = 2; | let a=1;let b=2; |
| function f() { ⏎ return ⏎ 42 ⏎ } | function f(){return ⏎ 42} |
| a = b ⏎ ++c | a=b ⏎ ++c |
| items ⏎ .map(f) ⏎ .join(",") | items.map(f).join(",") |
| const s = "a b"; // note | const s="a b"; |
| let t = `a ${b}` | let t=`a ${b}` |
| x = a + ++b | x=a+ ++b |
| const big = 1 .toString() | const big=1 .toString() |
| if (ok) /a b/.test(s) | if(ok)/a b/.test(s) |
| if (ok) {} ⏎ /a b/.test(s) | if(ok){}/a b/.test(s) |
| #!/usr/bin/env node ⏎ console.log( 1 ) | #!/usr/bin/env node ⏎ console.log(1) |
| /*! v1.0 MIT */ ⏎ var x = 1 | /*! v1.0 MIT */ ⏎ var x=1 |
⏎ marks a line break. A break stays where the next line would otherwise join the statement before it. The if (ok) {} row shows the known limit: after a } the / is read as a division, so the two spaces inside the regular expression become one.
Frequently asked questions
- Does this JavaScript minifier rename variables?
- No. It removes comments and whitespace only. Names, numbers and the structure of the code stay as written, and dead code is not removed. A minifier that parses the code and renames local variables gives a smaller file; use one if your project has a build step.
- Why does the minified code still have line breaks?
- Each one stands in for a semicolon the source left out. let a = 1 and let b = 2 on two lines become let a=1 and let b=2, still on two lines: joined as let a=1 let b=2 the code would not parse. Write the semicolons (let a = 1;) and the output is one line, let a=1;let b=2;. A return on its own line also keeps its break, so return followed by 42 on the next line still returns undefined.
- Can minifying break my JavaScript?
- Removing comments and whitespace keeps the meaning, with one known limit. A regular expression written right after a }, or after a ) that does not close the head of an if, for, while or with, is read as a division, and a run of spaces inside it shrinks to one: if (ok) {} with /a b/.test(s) on the next line comes out as if(ok){}/a b/.test(s). Put such a regular expression in a variable first. The code is not checked, so invalid input stays invalid.
- Does it keep /*! license comments?
- Yes, by default. Comments that start with /*! or contain @license or @preserve stay on their own line at the top of the file or between statements. One inside a statement, as in var x = 1 /* @license MIT */ + 2, is removed with a warning, and the output is var x=1+2. Untick the box to drop them all: the example then goes from 452 to 224 characters, 50.4% saved.
- How large a JavaScript file can I minify?
- Up to 2,000,000 characters. Above 200,000 the work runs in a background worker, which stops after 5 seconds, and a new paste replaces a run that is still going. Open .js file takes a .js, .mjs, .cjs, .jsx or .txt file of up to 2,000,000 bytes and reads it in the browser.
Related Tools
View all toolsCSS Minifier
Paste CSS to remove comments and unneeded whitespace while keeping descendant spaces, calc() operators and /*! */ license comments, with the characters saved.
HTML Minifier
Paste HTML to drop comments and collapse whitespace. The space between inline elements stays, pre and textarea are kept, and inline CSS and JS can be minified.
SQL Query Formatter
Format SQL for Standard SQL, PostgreSQL, MySQL, SQLite or SQL Server, with keywords kept, upper-cased or lower-cased and 2-space, 4-space or tab indents.
XML Formatter
Format or minify XML with 2 spaces, 4 spaces or tabs. Comments, CDATA, DOCTYPE, attribute values and mixed content stay as written, up to 300,000 characters.
JS Object to JSON Converter
Paste a JavaScript object or array literal and get JSON. Unquoted keys, single quotes and trailing commas are accepted, and no code is run.
Keycode Checker
Press a key to see its event.key, event.code, location and modifiers, plus the deprecated keyCode and which numbers: Enter is 13, Space 32, A to Z 65 to 90.