1. Create a new file and save it with a “.js” file extension. For example – helloworld.js.
2. Type the following code in the file:
3. Save the file.
Now that you’ve written code for your Hello World program, let’s take a closer look at the code and how it works.
Running in Command-Line
To run the program using a command-line interface, follow these steps:
1. Open your computer’s command-line interface. Depending on your operating system, you may be using Terminal (MacOS, Linux) or Command Prompt (Windows).
2. Locate the directory where you saved the file. Use the ‘cd’ command to navigate to that directory.
3. Use the ‘node’ command followed by the file name to run the program. For example:
This should output the text “Hello, World!” in the console.
Running in a Web Browser
If you’re familiar with HTML, you can also run the program in a web browser. Follow these steps:
1. Create an HTML file and save it with a “.html” file extension.
2. In the HTML file, add a script tag and link to the JS file you just created:
3. Open the HTML file in your web browser.
The “Hello, World!” text will be displayed in the console of your developer tools.