What Is Node.js?
Node.js is a JavaScript runtime environment that allows us to execute JavaScript outside a web browser.
Understanding Node.js
Normally, JavaScript runs inside a browser such as Chrome. Node.js provides an environment to run JavaScript directly on a computer or server.
This means developers can use JavaScript for both frontend and backend development.
Where Does Node.js Run?
Browser
JavaScript runs inside the browser.
Node.js
JavaScript runs outside the browser.
Both can execute JavaScript, but they provide different environments and APIs.
What Can We Do With Node.js?
- Create web servers.
- Build REST APIs.
- Read and write files.
- Connect applications to databases.
- Develop chat and other real-time applications.
Practical Example
Samadhan wants to display a message using Node.js. Let's write a simple program.
Step 1: Create a File
Create a JavaScript file named about.js.
Step 2: Write the Program
let name = "Samadhan";
let technology = "Node.js";
console.log("Hello " + name);
console.log("I am learning " + technology);
Step 3: Execute the Program
node about.js
Step 4: Output
Hello Samadhan
I am learning Node.js
Program Explanation
-
let name = "Samadhan";stores a name in a variable. -
let technology = "Node.js";stores the technology name. -
console.log()prints the message in the terminal. -
node about.jsexecutes the file using Node.js.
How the Program Executes
JavaScript program
Executes the JavaScript statements
Hello Samadhan
Important Points
- Node.js is a runtime environment, not a programming language.
- JavaScript is the programming language used in Node.js.
- Node.js can run JavaScript without opening a browser.
- It is widely used for backend applications and APIs.
- Use the
node filename.jscommand to run a file.
Summary
Node.js lets us execute JavaScript outside the browser. We can use it to build servers, APIs, and backend applications. We created about.js and executed it using the Node.js command.