Yandex

Hello World in JavaScript
Using Console, Node.js & VS Code



Introduction

Every programming journey begins with a humble start — the "Hello, World!" program. In JavaScript, this tiny snippet introduces you to the syntax, output methods, and the environments in which JavaScript runs. Whether you're using the browser console, Node.js, or Visual Studio Code, this guide walks you through it all.

1. JavaScript Hello World in the Browser Console

This is the fastest and most accessible way to run JavaScript without any installation.

Step-by-Step:

  1. Open Google Chrome or any modern browser.
  2. Right-click anywhere on the page and choose Inspect.
  3. Go to the Console tab.
  4. Type the following code and press Enter:

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

Output:

Hello, World!

Explanation: console.log() is a JavaScript function used to print messages to the console — great for debugging or quick testing.


2. Hello World using Node.js

Node.js is a JavaScript runtime that allows you to run JS outside the browser, typically for backend development.

Step-by-Step:

  1. Install Node.js from the official website: https://nodejs.org.
  2. Open your terminal or command prompt.
  3. Create a new file named hello.js using a code editor or touch hello.js on terminal.
  4. Add the following code:

// hello.js
console.log("Hello, World from Node.js!");

To Run:


node hello.js

Output:

Hello, World from Node.js!

Explanation: When you run node hello.js, Node.js executes the script file and outputs the result to your terminal.


3. Hello World using Visual Studio Code (VS Code)

VS Code is a lightweight, powerful code editor perfect for writing and testing JavaScript projects.

Step-by-Step:

  1. Download and install VS Code.
  2. Open VS Code and create a new folder (e.g., HelloWorldProject).
  3. Create a file inside it named hello.js.
  4. Write the following code:

// hello.js
console.log("Hello from Visual Studio Code!");

Run the Script:

  1. Open the terminal in VS Code (use Ctrl + `).
  2. Run the script using:

node hello.js

Output:

Hello from Visual Studio Code!

Tip: If you see an error like 'node' is not recognized, make sure Node.js is installed and its path is added to the environment variables.


Advanced Hello World Examples

1. Using Alert in a Browser

This example pops up a dialog box in the browser.


alert("Hello, World!");

Note: This only works in a browser context — not in Node.js.

2. Writing to the HTML Document

This method injects content into the webpage itself.


document.write("Hello, World on the Web Page!");

To use this, include it in a <script> tag inside an HTML file.


<!DOCTYPE html>
<html>
<head><title>JS Hello World</title></head>
<body>
  <script>
    document.write("Hello, World on the Web Page!");
  </script>
</body>
</html>

Key Concepts You Just Learned

  • console.log(): Used to print messages in browser/Node.js console.
  • alert(): Used in browsers to show popup messages.
  • document.write(): Outputs text directly to the web page.
  • Execution Environments: Browser Console, Node.js, and VS Code.

Common Questions

Q: Can I run JavaScript without a browser?

Yes, by using Node.js or an online editor like Repl.it or JSFiddle.

Q: Is Node.js necessary to learn JavaScript?

No, but it's useful if you want to run JS outside the browser or build server-side applications.

Q: Why doesn't alert() work in Node.js?

Because alert() is part of the browser's window object and Node.js doesn't have a DOM.


Conclusion

Your first step into JavaScript is complete. You've learned not only how to write a basic Hello, World! but also how to execute it in different environments. From here, you can explore variables, functions, and beyond. The world of JS awaits — happy coding!



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M