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); 

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 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
4 different Ways to Get JavaScript OutputJavaScriptIntroduction to components and templates Part 1: Component MetadataAngularSocial Media Colors: LESS VariablesMiscellaneousList of WordPress directories functionsWordPressThe flex-grow, flex-shrink and flex-basis Properties in CSSCSSRecursion in JavaScriptJavaScript