Nearby lessons

3 of 21

🌟 Node.js – Creating Your First Program

📌 How to Run JavaScript Code in Node.js

Once you have installed Node.js successfully, it's time to write and run your first JavaScript program. In this tutorial, you'll learn 4 different ways to run JavaScript code:

📋 Ways to Run JavaScript

  • Using an Online Compiler
  • Using the Terminal (Command Prompt)
  • Using a Web Browser
  • Using Visual Studio Code (VS Code)
Node.js Environment
RUNTIME ACTIVE
console.log("Hello, Node.js!");
console.log("This is your first Node.js program running in the terminal.");
Terminal Output
Live Output Preview

📌 Example: First JavaScript Program

Let’s start with a simple program in Node.js:

console.log("Hello, Node.js!");

This program will print:

Hello, Node.js!

✅ 1. Run JavaScript Using an Online Compiler

  • If you don’t want to install anything, you can use an online compiler.
  • Popular online compilers:
  • JSFiddle
  • CodeSandbox
  • Replit
  • 👉 Just paste the code and click Run.

✅ 2. Run JavaScript in the Terminal (Node.js)

After installing Node.js:

  1. Open Command Prompt (Windows) or Terminal (Mac/Linux).
  2. Create a new file named index.js.
  3. Add the following code:

✅ 3. Run JavaScript in the Web Browser

Since JavaScript was originally made for browsers, you can still run it there.

Create an HTML file and add JavaScript inside the <script> tag.

✅ 4. Run JavaScript in Visual Studio Code (VS Code)

VS Code is the most popular editor for Node.js development.

  1. Open VS Code.
  2. Create a file index.js.
  3. Write the following code:

🧠 Test Your Knowledge

4 Questions

Progress: 0 / 4
Keep Going!Node.js Modules Explained