How to run JavaScript?

Introduction

JavaScript is one of the most widely used programming languages in the world. Whether you’re building interactive websites or server-side applications, JavaScript plays a crucial role. However, before you can start building projects, you need to understand how to run JavaScript code.

There are two primary ways to run JavaScript:

  • In a web browser (client-side)
  • Using Node.js (server-side)

In this article, you’ll learn both methods step by step, along with examples, real-life use cases, common mistakes, and interview questions.

What Does It Mean to Run JavaScript?

Running JavaScript means executing code that performs actions such as displaying output, handling events, or processing data.

JavaScript code is executed by a JavaScript engine, such as:

  • Google Chrome’s V8 engine
  • Firefox’s SpiderMonkey
  • Node.js runtime (also uses V8)

Why Do We Need to Run JavaScript?

Running JavaScript allows developers to:

  1. Build interactive web pages
  2. Validate user input
  3. Create dynamic UI components
  4. Perform calculations and logic
  5. Build backend applications using Node.js

Without executing JavaScript, your code is just text—it won’t perform any action.

How to Run JavaScript in the Browser

Running JavaScript in the browser is the easiest and most common method, especially for beginners.

1. Using the <script> Tag

You can include JavaScript directly inside an HTML file using the <script> tag.

Example:


<!DOCTYPE html>
<html>
<head>
   <title>Run JavaScript</title>
</head>
<body>

<h2>Hello World</h2>

<script>
   console.log("JavaScript is running in the browser!");
</script>

</body>
</html>

How it Works:

  1. The browser reads the HTML file
  2. When it finds the <script> tag, it executes the JavaScript code
  3. Output can be seen in the browser console

2. Using an External JavaScript File

Instead of writing JavaScript inside HTML, you can use a separate .js file.

Step 1: Create script.js


console.log("External JavaScript file is running!");

Step 2: Link it in HTML


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

What are the benefits:

  1. Cleaner code
  2. Better maintainability
  3. Reusable scripts

3. Using Browser Console

You can run JavaScript instantly using the browser’s developer tools.

Steps:

  1. Open your browser (Chrome, Edge, etc.)
  2. Press F12 or Ctrl + Shift + I
  3. Go to the Console tab
  4. Type JavaScript code and press Enter

Example:


alert("Hello from Console!");

4. Using Inline JavaScript (Not Recommended)

JavaScript can be written directly inside HTML elements.

Example:


<button onclick="alert('Clicked!')">Click Me</button>

Why Avoid It:

  1. Hard to maintain
  2. Mixes HTML and JavaScript
  3. Not scalable

How to Run JavaScript Using Node.js

Running JavaScript outside the browser is possible using Node.js. It allows you to build backend applications, APIs, and tools.

What is Node.js?

Node.js is a runtime environment that allows you to run JavaScript on the server. It uses the V8 engine and is widely used for backend development.

1. Install Node.js

There are 3 steps to install Node.js

  1. Visit the official website
  2. Download and install Node.js
  3. Verify installation

Command:


node -v

2. Run JavaScript File

Step 1: Create a file app.js


console.log("Hello from Node.js!");

Step 2: Run the file


node app.js

Output:

Hello from Node.js!

3. Using Node REPL

Node.js provides an interactive shell called REPL.

Steps:

  1. Open terminal
  2. Type:

node

  • Start writing JavaScript:

console.log("REPL mode");

4. Running JavaScript with the Command Line

You can run JavaScript directly without creating a file.


node -e "console.log('Hello World')"

Browser vs Node.js

Feature Browser Node.js
Environment Client-side Server-side
Access to DOM Yes No
File System Access No Yes
Use Case UI, Interaction Backend, APIs
Example Button click Server request

Common Mistakes

There are many common mistakes made by Beginners.

1. Forgetting to Link JavaScript File

Wrong way:


<script>app.js</script>

✔ Correct:


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

2. Running Browser Code in Node.js


document.getElementById("id"); // ❌ Error in Node.js

3. Not Installing Node.js Properly

Always verify installation using:


node -v

Interview Questions

Q 1: How can you run JavaScript in a browser?
Ans: Using <script> tag, external files, or browser console.
Q 2: What is Node.js?
Ans: Node.js is a runtime environment that allows JavaScript to run outside the browser.
Q 3: Difference between browser and Node.js?
Ans: Browser runs client-side with DOM access, while Node.js runs server-side with system-level access.
Q 4: What is REPL in Node.js?
Ans: Read-Eval-Print Loop, an interactive environment to run JavaScript code.
Q 5: Can we use DOM in Node.js?
Ans: No, Node.js does not support DOM.
Q 6: How to check Node.js version?
Ans: node -v

Conclusion

Running JavaScript is the first step toward becoming a web developer. You can run JavaScript either in a browser for frontend development or using Node.js for backend development.

For beginners, starting with the browser is easier. As you grow, learning Node.js will help you build full-stack applications.

Mastering both environments will give you complete control over web development and open up many career opportunities.

Related JavaScript Tutorials