How to Write a Simple Hello World Program

Introduction

This tutorial will guide you through creating a simple "Hello World" program in JavaScript. We will use different HTML elements to display and format the code, including <pre>, <code>, <kbd>, <samp>, and <var>.

Step 1: Writing the Code

To start, open your code editor and create a new file called helloWorld.js. Inside this file, write the following JavaScript code:


            // This is a simple Hello World program
            var greeting = "Hello, World!";
            console.log(greeting);
        

The code defines a greeting variable and logs it to the console using console.log().

Step 2: Running the Program

To run the program, open your terminal and navigate to the directory where helloWorld.js is located. Then, type the following command:

node helloWorld.js

This will execute the JavaScript file using node.js and print the output in the terminal.

Output

After running the program, you should see the following output in your terminal:

Hello, World!

Conclusion

Congratulations! You've just written and executed your first JavaScript program. By using <pre>, <code>, <kbd>, <samp>, and <var>, you can format code, show keyboard input, display output, and highlight variables in your tutorials.