Fundamentals

What is JavaScript obfuscation?

JavaScript obfuscation is an automatic rewrite of working code that keeps its behaviour and removes its readability. The browser runs the result exactly as before. A person opening it in DevTools finds meaningless names, encoded strings and, with stronger settings, a structure that no longer resembles the source.

It exists because JavaScript ships as source. Anything a browser can run, a visitor can open, copy and modify. Obfuscation does not change that; it changes how much work understanding the code takes.

A real before-and-after

This is a small function and its output from our engine, unedited. First the source:

function calculateDiscount(customer, cartTotal) {
  var loyaltyRate = customer.loyaltyLevel * 0.05;
  if (customer.coupon === "WELCOME10") {
    loyaltyRate += 0.10;
  }
  return Math.min(cartTotal * loyaltyRate, 50);
}
console.log(calculateDiscount({ loyaltyLevel: 2, coupon: "WELCOME10" }, 120));

With name replacement, string encoding and strings moved into a table:

var _$_3de5=["\x57\x45\x4C\x43\x4F\x4D\x45\x31\x30"];function calculateDiscount(b,a){var c=b.loyaltyLevel* 0.05;if(b.coupon=== _$_3de5[0x0]){c+= 0.10};return Math.min(a* c,50)}console.log(calculateDiscount({loyaltyLevel:2,coupon:_$_3de5[0x0]},120))

Local names and the coupon string are gone. Notice what is still there: the top-level function name, and the property names loyaltyLevel and coupon. Those are kept by default because other scripts and your own data may depend on them. Turning on global renaming and member moving hides them too:

var a=["\x6C\x6F\x79\x61\x6C\x74\x79\x4C\x65\x76\x65\x6C","\x63\x6F\x75\x70\x6F\x6E","\x57\x45\x4C\x43\x4F\x4D\x45\x31\x30","\x6D\x69\x6E","\x6C\x6F\x67"];function b(d,c){var e=d[a[0x0]]* 0.05;if(d[a[0x1]]=== a[0x2]){e+= 0.10};return Math[a[0x3]](c* e,50)}console[a[0x4]](b({loyaltyLevel:2,coupon:a[0x2]},120))

All three versions print 24. Even here one thing survives in plain text: the keys of the object literal the caller passes in. That is the honest shape of obfuscation. Each option removes a class of clue, and you choose how many to remove. You can run the same sample yourself in the online obfuscator.

What obfuscation changes

  • Names. Variables, parameters and functions become short, meaningless identifiers. See name mangling.
  • Strings. Text is encoded, moved into a lookup table, or encrypted so that searching the file for a message or URL finds nothing. See moving strings into an array and string encryption.
  • Property access. obj.price can become a table lookup, which hides which members the code touches.
  • Structure. Control flow flattening turns readable branches and loops into a state machine; dead code insertion adds paths that never run.
  • Runtime behaviour. Optional defences make the code react to tampering, an open debugger, or the wrong domain. See runtime defense.

The transforms side by side page shows each one on the same input, and obfuscation techniques explained goes through how they work.

What it does not do

  • It is not encryption. The code must run, so everything needed to run it is in the file. See obfuscation vs encryption.
  • It does not hide secrets. An API key in browser code can be read from the network tab regardless. See you cannot hide an API key in JavaScript.
  • It does not replace server-side checks. A price, a permission or a licence decision made in the browser can be changed in the browser.
  • It is not irreversible. Formatting comes back with one click and strings can be decoded by running the code. Names, comments and original structure do not come back. See is obfuscation reversible? and can AI reverse it?

Obfuscation is not minification

A minifier makes code smaller: it shortens local names and removes whitespace, and a beautifier undoes most of that immediately. An obfuscator's goal is the opposite of convenience. It also hides strings, members and structure, and it will make a file larger if that makes it harder to follow. The two are usually used together. More in minification vs obfuscation.

What it costs

We measured twelve real libraries (511 KB of source) through our own engine:

  • Renaming plus string encoding: output about the same size as the input, raw and gzipped.
  • Adding self-compression: about half the size on disk, but roughly 40% larger once gzipped, because a compressed payload cannot be compressed again by the web server.
  • Runtime defences: each adds a few kilobytes of fixed code, which matters on small files and disappears on large ones.
  • Down-levelling modern syntax to ES5 adds code of its own; choose modern output if you do not need old browsers.

Speed follows the same pattern: renaming costs nothing at run time, while flattening and defences trade some execution time for resistance. Details and method are in how much bigger does obfuscation make your bundle? and does obfuscation slow down JavaScript?

When it is worth doing

Obfuscation earns its place when the code itself has value or exposes how something works: licensing and trial logic, pricing or scoring algorithms, anti-cheat and anti-bot checks, proprietary widgets shipped to customer sites, and code that would otherwise document your internal API for a competitor. It is rarely worth it for a marketing page, or as the only protection for something that should have been a server call.

How to try it

  1. Paste a file into the online obfuscator and compare the output. No account is needed.
  2. For a project, run it after your build: npx javascriptobfuscator-com --input dist --output dist-protected --preset free. See the npm CLI guide.
  3. Run your tests against the protected build before you ship it. Testing obfuscated JavaScript explains what to check.

The step-by-step version is in how to obfuscate JavaScript. For scripts written directly in a page, see how to obfuscate JavaScript inside an HTML file.

Frequently asked questions

What is JavaScript obfuscation in simple terms?

It is an automatic rewrite of JavaScript that keeps the behaviour and removes the readability. Names become meaningless, strings are encoded or moved into a lookup table, and the structure can be rearranged. The browser runs the result exactly as before; a person reading it has far less to go on.

Is obfuscated JavaScript encrypted?

No. Encrypted data is unreadable without a key. Obfuscated JavaScript must still run in the browser, so everything needed to run it ships with it. Obfuscation raises the time and skill needed to understand the code; it does not make the code secret.

Can obfuscated JavaScript be reversed?

Partly. Formatting can always be restored and encoded strings can be decoded by running the code. Original names, comments and structure are gone and cannot be recovered, only guessed. How long a determined analyst needs depends on which transforms were used.

Does obfuscation make JavaScript bigger or slower?

Renaming and string encoding leave size about the same and have no measurable speed cost in ordinary code. Heavier options cost more: self-compression halves the file on disk but makes the gzipped download larger, and control-flow flattening and runtime defences add code and some execution time. Measure with your own bundle.

What should never rely on obfuscation?

Secrets and authorization. An API key, a password or a licence check that the browser can evaluate can be extracted from the browser, obfuscated or not. Keep secrets and permission checks on the server and use obfuscation for the code that has to ship.

Related reading