Yogesh Chauhan's Blog

How to obfuscate JavaScript code to hide it from View Source?

in JavaScript on November 7, 2020

What is Obfuscation?

Obfuscation is the technique to hide the intended meaning of message/code/text by making the message difficult to understand, usually with confusing and ambiguous language.

For e.g. let’s say this is our function in JavaScript.


function test(name) {
 console.log('Hello, ' + name); 
} 
hello('Yogesh');

For that, this is obfuscated code:


eval(function(p,a,c,k,e,d){e=function(c){return c};if(!''.replace(/^/,String)){while(c--){d[c]=k[c]||c}k=[function(e){return d[e]}];e=function(){return'\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\b'+e(c)+'\b','g'),k[c])}}return p}('2 1(0){3.7('4, '+0)}5('6');',8,8,'name|test|function|console|Hello|hello|Yogesh|log'.split('|'),0,{}))

Remember that the obfuscated code depends on which tool you use to obfuscate your code.

There are many tools available to obfuscate the JS code for e.g. Google Closure Compiler

You can also install tool on your local machine. In this article, I will go through the step by step guide on how to install obfuscate tool on your local machine.

Step 1

Make sure you have node and npm are installed on your machine. Check the version using this command:


node --version

npm --version

Step 2

The obfuscator tool should be installed globally. So, we can use -g for that.

Use npm with the -g parameter and sudo (admin user) to install it.


sudo npm install -g  javascript-obfuscator

// it will ask for admin password

It will get installed in few seconds and you’ll get a similar message like this one:


+ [email protected]
added 110 packages from 160 contributors 
in 5.6s

Step 3

Use obfuscator tool to obfuscate your JS code.

Go the folder where your JS code file is and type this command in CLI.


javascript-obfuscator .

You’ll see the new obfuscated file in the same folder within seconds.

That’s it.

There are customization options available to use.

Find out more on: npm


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
Array destructuring and Object destructuring in JavaScriptJavaScriptArguments in @mixin rules in SCSS (Sass)SCSSAlternate Style Sheets in CSSCSSSQL Left JoinSQL/MySQLDebugging in WordPress Part 2: WP_DEBUG_LOG and WP_DEBUG_DISPLAYWordPressHow to add Google Static Map using ACF map field?WordPress