Node.js is used to execute JavaScript code outside of a web browser and helps to build server-side applications. Let us explore a few Node.js examples.
Below is a very simple example that prints a message using console.log().
console.log('Hello Tpoint Tech');
Output:
Hello Tpoint Tech
Explanation:
The console.log() function simply displays a message in the console.
Let us look at another example where we will perform the addition operation.
Code:
const firstNumber = 20; const secondNumber = 30; const addition = firstNumber + secondNumber; console.log(addition);
Output:
50
Explanation:
We have taken two variables firstNumber and secondNumber. The variable firstNumber stores the value 20 and the variable secondNumber stores the value 30. We have added both the numbers and stored the result in the variable called addition then printed the addition of both the numbers.
We will look at an example in which we learn how to declare variables and display their values in Node.js. Variables are used to store data that can be accessed and utilized throughout a program.
Code:
const name = "John";
const age = 25;
console.log("Name:", name);
console.log("Age:", age);
Output:
Name: John Age: 25
Explanation:
We have declared two variables named name and age using the const keyword. The variable name stores the value "John" while the variable age stores the value 25. The console.log() function is then used to display both values in the console.
We will utilize an if-else statement for checking whether a student has passed or failed on the basis of their marks.
Code:
const marks = 85;
if (marks >= 40) {
console.log("Pass");
} else {
console.log("Fail");
}
Output:
Pass
Explanation:
We have a variable called marks that stores the value 85. The if statement checks whether the marks are greater than or equal to 40. Since the condition evaluates to true so the message "Pass" is displayed. If the marks are less than 40 then the else block would execute and display "Fail".
This example shows how functions can be utilized to organize and reuse code in Node.js applications. We will create a simple function that accepts a name as a parameter and returns a greeting message.
Code:
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("John"));
Output:
Hello, John!
Explanation:
Here, a function named greet() is created with a parameter called name. When the function is called with the value "John" then the function returns the greeting message "Hello, John!".
We will create a file in this example and write content to it using the fs.writeFile() method.
Code:
const fs = require('fs');
fs.writeFile('sample.txt', 'Welcome to Tpoint Tech', (err) => {
if (err) {
console.log(err);
return;
}
console.log('File created successfully.');
});
Output:
File created successfully.
Explanation:
We have imported the fs module using the require() function. Here, the writeFile() method creates a file named sample.txt and writes the text "Welcome to Tpoint Tech" into it. If an error occurs during the operation then it is displayed on the console. Otherwise the message "File created successfully" is printed on the console.
Let us look at an example that displays the name of the current file utilizing the path module.
File: index.js
const path = require('path');
console.log(path.basename(__filename));
Output:
index.js
Explanation:
The path module is imported using the require() function. The __filename variable contains the complete path of the currently executing file. The basename() method extracts only the file name from that path and returns index.js which is then displayed on the console.
We will retrieve information about the operating system and the system architecture in the following example.
Code:
const os = require('os');
console.log(os.platform());
console.log(os.arch());
Output:
win32 x64
Explanation:
The os module is imported using the require() function. The platform() method returns the operating system platform while the arch() method returns the system architecture.
We request you to subscribe our newsletter for upcoming updates.