Your First JavaScript Program

JavaScript runs inside your web browser to add interactivity to web pages. Your first JavaScript program will display a message in the browser console.

To write JavaScript, you can use the <script> tag inside an HTML file or run code directly in the browser’s developer console.

📌 Deep Dive: Hello World in JavaScript

JAVASCRIPT
console.log('Hello, World!');
Output
Hello, World!

This code sends the message Hello, World! to the browser’s console.

💡 Running Your Code

Open your browser, press F12 or Ctrl+Shift+I (Cmd+Option+I on Mac) to open Developer Tools, then go to the Console tab to run JavaScript directly.

Embedding JavaScript in an HTML file looks like this:

📌 Deep Dive: Embedding JavaScript

HTML
<script>
  console.log('Hello, World!');
</script>

⚠️ Important

JavaScript code inside <script> runs as soon as the browser loads it. Place scripts at the bottom of the HTML body or use defer attribute to avoid blocking page rendering.

JavaScript can also display messages on the webpage itself, not only in the console. The alert() function shows a popup box:

📌 Deep Dive: Alert Popup

JAVASCRIPT
alert('Hello, World!');

Use console.log() for debugging and alert() for simple user notifications.

console.log() vs alert()
console.log()alert()
Logs message to browser consoleShows popup alert box
Non-intrusive, used for debuggingBlocks user interaction until dismissed
Used by developersSeen by users
Illustration of Your First JavaScript Program
Illustration of Your First JavaScript Program

Now you know how to write and run your first JavaScript program! Practice by changing the message inside console.log() or alert().