How do I Run Code I have Written in JavaScript?

To execute JavaScript code, you must employ an environment that can interpret and execute JavaScript code. It can be executed in a variety of ways. There are numerous online code editors available that allow you to write and execute JavaScript code in a browser window, such as a browser console, CodePen, and so on. You can also run/execute the JavaScript code in a command line interface or link the file with an HTML file.

This blog will demonstrate the ways to run the code written in JavaScript.

How to Run JavaScript Code?

To run your code written in JavaScript, use the following ways/solutions:

Solution 1: Run JavaScript Code on Browser Console

You can run your JavaScript code on the browser console. To do so, press the “F12” key or the “Ctrl + Shift + I”:

Enter your code and press the “Enter” key. Let’s see an example, to run the code on the browser console.

Create a variable “message” and store a string in it:

var message = "Welcome To Linuxhint JavaScript Tutorials";

Print the message by calling the “console.log()” method:


After executing the above code, the output will be as follows:

You can also perform arithmetic operations in JavaScript on the console. Create two variables “x” and “y” and store values “25” and “5” respectively:

var x = 25;
var y = 5;

Multiply “x” and “y” using the operator “*” and store the result in the variable “product”:

var product = x * y;

Print the resultant value on the console:



Note: If you have a JavaScript code file with “.js” extension, then move to the second solution.

Solution 2: Run JavaScript Code Linking with HTML File

You can also run the JavaScript code by linking it with the HTML file using the <script> tag with the “src” attribute in the <head> tag:

<script src="./JSfile.js"></script>

We have written the following code in the “JSfile.js” and linked it in a <head> tag of an HTML file.

For printing message on the console:

var message = "Welcome To Linuxhint JavaScript Tutorials";

For finding the product of two numbers “25” and “5”:

console.log("Find the product of two numbers in JavaScript");
var x = 25;
var y = 5;
var product = x * y;
console.log("Product of 25 and 5 is " + product);

It can be seen that the code has been successfully executed by linking the JS file with the HTML file:

Note: You can also run your JavaScript code using different frameworks and also you can run it from the “Node.js” command-line interface.


To run the JavaScript code, you can use online editors, such as “CodePen” or also use the browser console. You can link or attach your JavaScript file with the HTML file or use the “Node.js” command-line interface. This blog demonstrated different ways to run JavaScript code.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.