0
0
Javascriptprogramming~5 mins

Running JavaScript using Node.js

Choose your learning style9 modes available
Introduction

Node.js lets you run JavaScript outside the browser. This helps you build programs and tools on your computer.

You want to run JavaScript code directly on your computer without a browser.
You are building a server or backend service using JavaScript.
You want to test small JavaScript snippets quickly.
You need to run scripts to automate tasks on your computer.
You want to learn JavaScript beyond web pages.
Syntax
Javascript
node filename.js

Replace filename.js with your JavaScript file name.

Run this command in your terminal or command prompt where the file is saved.

Examples
This runs the JavaScript file named app.js.
Javascript
node app.js
This runs the JavaScript file named script.js.
Javascript
node script.js
Running node alone opens an interactive prompt where you can type JavaScript commands directly.
Javascript
node
Sample Program

This simple program prints a message to the console when run with Node.js.

Javascript
console.log('Hello from Node.js!');
OutputSuccess
Important Notes

Make sure Node.js is installed on your computer before running commands.

You can download Node.js from nodejs.org.

Use the terminal or command prompt to run Node.js commands.

Summary

Node.js runs JavaScript outside the browser on your computer.

Use node filename.js to run a JavaScript file.

You can also run node alone to try JavaScript interactively.