Secure Your Javascript Code with Obfuscation

Tuhin Das
Webtips
Published in
3 min readJun 25, 2020
Javascript Code Obfuscation

In this story, I will cover code obfuscation, it’s importance in javascript and how it is different from code minification.

Code Obfuscation is a technique of transforming the executable code, making it almost impossible to read and understand without affecting the overall functionality of that code.

Why Code Obfuscation in Javascript is important?
Javascript code mostly runs on browsers and is easily accessible to anyone.

Due to this it always possesses following threats:

  • Code can be copied and used without permission.
  • It makes your code vulnerable against hackers.
  • Functional logic and algorithms are exposed.

Applying Code Obfuscation in Javascript

Let’s consider the above code snippet in Javascript, after applying code obfuscation it will look like below code:

You can always select the level of obfuscation you want to apply. With deeper code obfuscation, code will be more safe and secure. Below is an example of a deeper code obfuscation on the same code snippet:

Code Obfuscation VS Code Minification

Code obfuscation may look a lot similar to minification or uglification but they are different from each other.

Code minification is a process of compressing your source code in order to save memory by reducing characters, spaces etc. in the code.

Minified code can be easily converted back to its original form using any code beautifying tool but, in case of obfuscation you need to use the same tool with exact rules which was used to obfuscate. This process is called deobfuscation. Let’s understand with the help of following code snippet:

After minification, the above code will look something like below:

As you can see, even though the code is transformed and some extra characters are removed, but the logic is still easily understandable. This is not the case with obfuscation. After obfuscation, the code will look something like below which is almost impossible to understand.

Tools For Code Obfuscation In Javascript

There are lot of tools available for code obfuscation in javascript. You can use an online tool like:

Or npm packages like npm javascript obfuscator or webpack-obfuscator

Obfuscation With Performance

Although obfuscation protects your code but it also makes your code slower when compared to its original form due to the extra piece of code which it uses to hide actual implementation. So, it’s always important to identify the crucial parts of your code and be selective in applying obfuscation. This approach will help you in keeping a balance between code protection and performance.

Try obfuscating your code and if you find something interesting or any issue, do let me know in response section.

--

--

Tuhin Das
Webtips
Writer for

Front End Engineer (L5) @ Amazon | Solving Customer Problems In E-Commerce Domain