Skip to main content
CalcChef

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

InputOutput
let a = 1 ⏎ let b = 2let 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 ⏎ ++ca=b ⏎ ++c
items ⏎ .map(f) ⏎ .join(",")items.map(f).join(",")
const s = "a b"; // noteconst s="a b";
let t = `a ${b}`let t=`a ${b}`
x = a + ++bx=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 tools