node js code for saving first middle and last name
const fs = require('fs');
const saveName = (firstName, middleName, lastName) => {
const fullName = `${firstName} ${middleName} ${lastName}`;
fs.writeFile('name.txt', fullName, (err) => {
if (err) {
console.error(err);
return;
}
console.log('Name saved successfully!');
});
};
saveName('John', 'Doe', 'Smith');
- First, we import the
fs
module, which stands for file system. This module provides functions for interacting with the file system in Node.js. - Next, we define a function called
saveName
that takes three parameters:firstName
,middleName
, andlastName
. - Inside the
saveName
function, we create afullName
variable by concatenating the three name parameters with spaces in between. - We then use the
fs.writeFile
function to write thefullName
to a file calledname.txt
. This function takes three parameters: the file name, the data to be written, and a callback function that will be called when the operation is complete. - If there is an error during the file write operation, we log the error to the console.
- If the operation is successful, we log a success message to the console.
- Finally, we call the
saveName
function with some example values ('John', 'Doe', 'Smith') to save the name to the file.
This code will save the full name (first name, middle name, and last name) to a file called name.txt
using the Node.js fs
module.