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:
- Build interactive web pages
- Validate user input
- Create dynamic UI components
- Perform calculations and logic
- 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:
- The browser reads the HTML file
- When it finds the <script> tag, it executes the JavaScript code
- 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:
- Cleaner code
- Better maintainability
- Reusable scripts
3. Using Browser Console
You can run JavaScript instantly using the browser’s developer tools.
Steps:
- Open your browser (Chrome, Edge, etc.)
- Press F12 or Ctrl + Shift + I
- Go to the Console tab
- 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:
- Hard to maintain
- Mixes HTML and JavaScript
- 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
- Visit the official website
- Download and install Node.js
- 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:
3. Using Node REPL
Node.js provides an interactive shell called REPL.
Steps:
- Open terminal
- 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?
Q 2: What is Node.js?
Q 3: Difference between browser and Node.js?
Q 4: What is REPL in Node.js?
Q 5: Can we use DOM in Node.js?
Q 6: How to check Node.js version?
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.