Skip to content

JavaScript Minifier - Free Online JS Code Optimizer

A free browser-based JavaScript minifier that removes comments and unnecessary whitespace while preserving semicolons, strings, and code behavior exactly.

JavaScript Minifier

About this tool

This simple JavaScript minifier removes unnecessary whitespace and comments to reduce the size of your code. It preserves functionality while making your code more compact.

  • Removes unnecessary whitespace (spaces, tabs, newlines)
  • Removes comments (both single-line and multi-line)
  • Preserves string literals and regular expressions
  • Maintains code functionality
Loading calculator...
📚

Documentation

What is a JavaScript minifier?

A JavaScript minifier is a tool that shrinks JavaScript source code by removing characters a browser does not need to run it, such as extra spaces, line breaks, and comments. The code still does exactly the same thing after minification. Only its file size changes.

Smaller JavaScript files download and load faster, so minifying is a common step before a website goes live.

How JavaScript minification works

This tool reads code one character at a time and rebuilds it, dropping anything that readers need but the JavaScript engine does not. It removes:

  • Comments, both // single-line comments and /* multi-line */ comments, including JSDoc-style comments.
  • Extra spaces, tabs, and blank lines that only exist to make code easier to read.

It does not add or remove anything else. It never deletes a semicolon, a parenthesis, or any other character that was already in the code, and it never inserts new punctuation. It also leaves several things untouched:

  • Text inside strings ("like this", 'like this') and template literals (`like this`), even if that text contains spaces or looks like a comment.
  • Regular expression literals, such as /[a-z]+/g.
  • Every semicolon, comma, and bracket the original code already had.

Sometimes a space between two tokens cannot be removed without changing their meaning. return a cannot become returna, because that would read as a single word. When removing a space would fuse two tokens together, the tool keeps a single space instead.

A line break can also matter. JavaScript sometimes inserts a semicolon automatically at the end of a line, a rule called Automatic Semicolon Insertion. If deleting a line break could trigger or block that rule, or if the line break follows a closing brace }, the tool keeps a single newline there. This is why minified code is not always squeezed onto one line: some line breaks change what the code means and have to stay.

How to use this JavaScript minifier

  1. Paste JavaScript code into the input box.
  2. Select "Minify".
  3. Read the result in the output box, along with the original size, the minified size, and the percentage reduction.
  4. Select "Copy" to copy the minified code.

Processing happens in the browser tab. The code is never sent to a server.

Example: minifying a function

Before:

1// Calculate the sum of two numbers
2function addNumbers(a, b) {
3    // Return the sum
4    return a + b;
5}
6
7// Call the function with 5 and 10
8const result = addNumbers(5, 10);
9console.log("The sum is: " + result);
10

After:

1function addNumbers(a,b){return a+b;}
2const result=addNumbers(5,10);console.log("The sum is: "+result);
3

Both comments disappear and the indentation is gone. The semicolon after a+b stays, because the tool never deletes a semicolon that was already there. The line break before const also stays, because it follows a closing }.

Example: minifying a class

Before:

1/**
2 * A simple counter class
3 * that increments and decrements a value
4 */
5class Counter {
6    constructor(initialValue = 0) {
7        this.count = initialValue;
8    }
9
10    increment() {
11        return ++this.count;
12    }
13
14    decrement() {
15        return --this.count;
16    }
17
18    getValue() {
19        return this.count;
20    }
21}
22
23// Create a new counter
24const myCounter = new Counter(10);
25console.log(myCounter.increment()); // 11
26console.log(myCounter.increment()); // 12
27console.log(myCounter.decrement()); // 11
28

After:

1class Counter{constructor(initialValue=0){this.count=initialValue;}
2increment(){return++this.count;}
3decrement(){return--this.count;}
4getValue(){return this.count;}
5}
6const myCounter=new Counter(10);console.log(myCounter.increment());console.log(myCounter.increment());console.log(myCounter.decrement());
7

Notice that a line break stays before each method and before the class's closing }. Every one of those breaks follows a closing } in the original code, so the rule that protects Automatic Semicolon Insertion keeps them. The semicolons inside each method also stay.

Limits of this minifier

This tool only removes comments and whitespace. Unlike build-step tools such as Terser or UglifyJS, it does not rename variables, delete unused code, or combine files. Those tools usually produce smaller output but need a build process to run. This tool works instantly in the browser with no setup, which suits quick checks or small snippets rather than a full production pipeline.

Frequently asked questions

What does a JavaScript minifier remove?

It removes comments and whitespace that are not needed for the code to run. It does not remove semicolons, brackets, or any other character that was already part of the code.

Does minifying JavaScript change how it runs?

No. Minified code should produce the same results as the original. Only the formatting changes.

Why does minified code sometimes still have line breaks?

A line break can be required by JavaScript's Automatic Semicolon Insertion rule, or it can follow a closing brace }. Removing it could change what the code does, so the tool keeps a single newline in those spots instead of deleting it.

Is minification the same as compression?

No. Minification removes characters from the source code itself. Compression, such as gzip or Brotli, encodes the file differently for transfer over a network. Websites commonly use both together.

Is my code sent to a server?

No. The minifier runs entirely in the browser tab. The code is never uploaded anywhere.

Can minified code be turned back into the original?

Whitespace can be restored by a code formatter, but comments and the original variable and function names are gone unless a separate source map was saved.

References

  1. Google Developers. "Minify Resources (HTML, CSS, and JavaScript)." Web Fundamentals, https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/optimize-encoding-and-transfer#minify_html_css_and_javascript
  2. MDN Web Docs. "JavaScript." Mozilla Developer Network, https://developer.mozilla.org/en-US/docs/Web/JavaScript
  3. ECMA International. "ECMAScript Language Specification," section 12.10, "Automatic Semicolon Insertion." https://tc39.es/ecma262/