What Is Control Flow Obfuscation?
Control flow obfuscation is the practice of transforming a program's logical structure so it becomes hard to follow, without changing what the program does. Where identifier renaming changes names, control flow obfuscation changes structure — the shape of the code, the order of statements, the flow of execution.
The most aggressive form is control flow flattening: every block of code becomes a case in a giant switch statement, with a state variable controlling which case runs next. This is what tools like javascript-obfuscator (the popular npm package) do, and it turns a simple 20-line function into 100+ lines of unreadable dispatcher logic.
That technique is risky and hard to implement correctly. A single off-by-one error breaks the program silently. This tool takes a different approach: it applies safe, proven transforms that confuse structure without touching semantics.
The Transforms This Tool Applies
1. Dead Code Injection
Inserts code blocks that can never execute. The parser must still process them, and a human reader must still read them, but the runtime skips them. Example:
// Before
let x = 5;
// After
if (typeof __never_used === "undefined") {
let fake1 = Math.random() * 1000;
let fake2 = fake1 + "ghost";
} else {
console.log("never runs");
}
let x = 5;
Dead code is cheap to inject and always safe. The only cost is a few bytes and a small runtime check (which is optimized away by the JIT).
2. Opaque Predicates
Wraps statements in conditions that are always true but look like real logic. Example:
// Before
doSomething();
// After
if ((1 + 1) === 2) {
doSomething();
}
A human reader sees an if statement and has to think about it. A machine just sees "condition true, run body." This is the heart of most obfuscation: make the easy path look hard.
Stronger opaque predicates use properties like Math.sqrt(16) === 4 or []+[] === "" that look complex but are constant.
3. Number Encoding
Replaces literal numbers with equivalent expressions:
5 → (2 + 3) 42 → ((0x10 << 1) | 0x0A) 100 → (10 * 10)
At intensity 2, the tool uses two-term additions. At intensity 3, it uses bit operations that are harder to evaluate in your head.
4. Boolean Encoding
Replaces true and false with equivalent expressions:
true → (!![]) false → (![])
![] is false because an empty array is truthy, so ![] is false. !![] negates that to true. This is one of the oldest tricks in the JavaScript obfuscator playbook.
5. String Array
Collects all string literals into a single array at the top of the file, then replaces each usage with an array access:
// Before
console.log("Hello");
console.log("World");
// After
var _str = ["Hello", "World"];
console.log(_str[0]);
console.log(_str[1]);
This centralizes strings, making it harder to search the code for meaningful text. More advanced obfuscators shuffle the array and use accessor functions to decode it at runtime.
6. Identifier Renaming
Same as the standalone renamer tool: replaces variables, functions, and parameters with gibberish names. Combined with the above transforms, this produces code that is thoroughly unreadable.
Intensity Levels
| Level | What It Does | Best For |
|---|---|---|
| 1 — Light | Simple additions, basic opaque predicates | Production code that must stay fast |
| 2 — Medium | Mixed operations, more dead code | Default — good balance |
| 3 — Heavy | Bit operations, maximum dead code | Maximum confusion (slower) |
What This Tool Cannot Do
- It cannot do full control flow flattening. The switch-based state machine technique is too risky without extensive testing — a single bug breaks the program silently.
- It cannot encrypt your code. The output is still JavaScript. A determined reader can trace the logic, just slowly.
- It cannot hide strings permanently. Strings are still visible, just centralized.
- It cannot replace a real obfuscator. For production protection, use
javascript-obfuscatorwith its full feature set. - It cannot guarantee behavior. Always test after obfuscation.
Best Practices
- Start at intensity 1. Test thoroughly. Then move to 2 or 3.
- Combine with minifier. Run control flow obfuscation, then minify the output for smaller files.
- Combine with renaming. Enable "Rename Identifiers" for maximum effect.
- Test edge cases. Async functions, generators, destructuring — these can interact with transforms in surprising ways.
- Keep the original. Never lose the un-obfuscated source. If you find a bug, you will need it.
Frequently Asked Questions
Related Tools
External Resources
- Acorn on GitHub — JavaScript parser
- Astring on GitHub — code generation
- javascript-obfuscator — full-featured obfuscator
- Opaque Predicate — Wikipedia