Open In App

JavaScript Hello World

Last Updated : 02 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The JavaScript Hello World program is a simple tradition used by programmers to learn the new syntax of a programming language. It involves displaying the text "Hello, World!" on the screen. This basic exercise helps you understand how to output text and run simple scripts in a new programming environment.

How to Write a Simple JavaScript Hello World Program

1. Using the Browser Console

One of the easiest ways to run JavaScript code is by using the browser's built-in console. Modern web browsers like Google Chrome, Firefox, and Edge provide developer tools that allow you to execute JavaScript code directly within the browser.

Steps:

  1. Open your web browser (e.g., Google Chrome).
  2. Press F12 or Ctrl+Shift+I (Windows/Linux) or Cmd+Opt+I (Mac) to open the Developer Tools.
  3. Go to the Console tab.
  4. Type the following JavaScript code:
JavaScript
console.log("Hello, World!");

2. Using an HTML File

You can also run JavaScript in a web page by embedding it inside an HTML document. This is the most common way JavaScript is used on websites.

Steps:

  1. Open any text editor (e.g., Notepad, Sublime Text, VS Code).
  2. Create a new file and save it with the .html extension (e.g., hello-world.html).
  3. Write the following code:
  4. Save the file and open it in a web browser.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello World in JavaScript</title>
</head>
<body>
    <script>
        alert("Hello, World!");
    </script>
</body>
</html>

3. Using an External JavaScript File

For larger applications, it’s a good practice to separate JavaScript code into external files. This helps with better organization and reusability.

Steps:

  1. Create two files:
    • index.html: The HTML file.
    • script.js: The JavaScript file.
  2. Write the following code in the hello-world.html file
  3. Save both files in the same directory and open the html file in a browser.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello World in JavaScript</title>
</head>
<body>
    <script src="script.js"></script>
</body>
</html>
script.js
console.log("Hello, World!");

Conclusion

The Hello World program in JavaScript is a simple yet essential way to learn the basic syntax and functionality of the language. By following the examples in this guide, you can easily start writing and running JavaScript code in your browser or by creating web pages with JavaScript embedded.


Next Article

Similar Reads