JS Execution
Alright! Now that we understand what JavaScript is and why we use it, the next big question is — how do we actually run JavaScript code? Let’s clear that up.
Do We Need to Install JavaScript?
The answer is NO! 😄
You don’t need to install JavaScript separately. It’s already built into every modern web browser — like Chrome, Firefox, Edge, or Safari. That means JavaScript is already present on your computer, in your phone’s browser, and even in most smart devices.
So yes, JavaScript is practically everywhere!
How to Execute JavaScript
There are a few different ways you can run JavaScript code, depending on what you want to do. Let’s look at the most common ones 👇
1. Run JavaScript in Your Browser Console
Every browser comes with a JavaScript console built into its Developer Tools.
Here’s how you can try it:
- Open your browser (for example, Chrome).
- Right-click anywhere on the page and select Inspect.
- Click on the Console tab.
- Type this code and hit Enter:
console.log("Hello, JavaScript!");
You’ll immediately see the output right there in the console. This is the easiest way to test small pieces of code quickly.
2. Run JavaScript Inside an HTML File
You can also run JavaScript directly inside your webpage using the <script>
tag.
Here’s an example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript Example</title>
</head>
<body>
<h1>Hello from CodeWithHarry!</h1>
<script>
alert("Welcome to JavaScript!");
console.log("This message is logged in the console!");
</script>
</body>
</html>
When you open this file in a browser, you’ll see a popup message and a log in your console. This is the most common way JavaScript is used in web development — directly inside an HTML page.
We can also run JavaScript outside the browser, and that we will see in the next tutorial.