Skip to main content

Command Palette

Search for a command to run...

How to Write a Hello World Program in JavaScript

Published
3 min read

When starting with JavaScript, one of the first programs you will write is the classic "Hello, World!" program. It is a simple way to get comfortable with basic syntax and understand how JavaScript interacts with a webpage or runs in a console.

Here is you will find the full code.

What You Will Need

Before you start, ensure you have the following:

  • A Code Editor: You can use any editor, but VS Code or Sublime Text are great options for beginners.

  • A Browser: Modern browsers like Chrome or Firefox are excellent because they have built-in developer tools.

  • Basic Knowledge: Familiarity with HTML will make this process smoother.

Writing Hello World in JavaScript

Using the Browser Console

This is the quickest way to run JavaScript. Follow these steps:

  1. Open any modern browser (e.g., Chrome).

  2. Open the Developer Tools. You can usually do this by right-clicking on the page and selecting Inspect, then navigating to the Console tab.

  3. Type the following code and press Enter:

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

You should see Hello, World! displayed in the console.

Adding JavaScript to an HTML File

To make your "Hello, World!" program more dynamic, you can link JavaScript to an HTML file. Here is how:

  1. Create a new file called index.html and add the following code:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello World</title>
</head>
<body>
    <h1 id="message">Welcome!</h1>
    <script src="script.js"></script>
</body>
</html>
  1. Create another file in the same folder called script.js and write:
document.getElementById('message').textContent = 'Hello, World!';
  1. Open index.html in a browser, and you will see the message "Hello, World!" replacing "Welcome!".

Writing JavaScript Inline in HTML

If you prefer to keep everything in one file, you can include JavaScript directly in the HTML file. Here is an example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello World Inline</title>
</head>
<body>
    <h1 id="inline-message">Welcome!</h1>
    <script>
        document.getElementById('inline-message').textContent = 'Hello, World!';
    </script>
</body>
</html>

Open this file in a browser, and you will see the same result.

Tips for Writing JavaScript

  • Use console.log(): This is a great tool for debugging and testing.

  • Use Comments: Add comments to explain what your code is doing. For example:

// This changes the text content of the element with id "message"
document.getElementById('message').textContent = 'Hello, World!';
  • Practice Often: Try changing the message or adding more dynamic features as you learn.

Conclusion

Writing a "Hello, World!" program in JavaScript is simple but essential for beginners. It introduces you to the basics of JavaScript and how it interacts with HTML. As you get more comfortable, you can build on this foundation to create more complex and interactive web applications.