Unfortunately, this makes the JS visible to users too. Anyone with slight experience in browser debugging can see the source code of JS files. Even the minified files can be reverted to their original source code to make them readable.
By default browsers/servers do not provide any mechanism to secure your JS code but there are some techniques by which you can protect your source code. Code Obfuscation is one of the best and robust technique to secure the JS code.
Let me show you how a simple JS and its obfuscated code looks like. The first one is the normal JS code and second one is obfuscated version –
How JS Obfuscation works ?
Obfuscation is the technique in which the tool scans your source code and changes the variable names, function names, data structures and rearranges the code in order to make it less readable and nearly impossible to apply reverse engineering on the code.
Benefits of code Obfuscation
- Protects the source code from being available to everyone.
- Works with almost all JS frameworks.
- Maintains the algorithmic logic and data structures.
- Reduces the size of code.
Drawbacks of code Obfuscation
- Obfuscation process is irreversible. So if you loose your original source code, you lost it.
- Can reduce the productivity in development and staging phases.
- Need special tools, where you can share your JS code to apply obfuscation technique.