How to Add Two Numbers in Console using Node.js ? Last Updated : 20 Jun, 2024 Comments Improve Suggest changes Like Article Like Report Adding two numbers using Node.js is a basic task that helps in understanding how to handle user input and process data within the Node.js environment. By following the steps in this guide, you'll learn how to create a simple Node.js application that reads numbers from the console, performs an addition operation, and displays the result. ApproachTo add two numbers in console using Node.js we will be using prompt package to get and process the input in console window. Then the string input is converted to float and added. The result is displayed using the console.log method. Installation StepsStep 1: Make a folder structure for the project. mkdir myappStep 2: Navigate to the project directory cd myappStep 3: Initialize the NodeJs project inside the myapp folder. npm init -yStep 4: Install the necessary packages/libraries in your project using the following commands. npm install promptProject Structure:The updated dependencies in package.json file will look like: "dependencies": { "prompt": "^1.3.0" }Example: Implementation to show how to add numbers in nodejs. Node // app.js // Require would make the prompt // package available to use const prompt = require("prompt"); // An utility function to add // two numbers function add() { // Start the prompt prompt.start(); // Get two numbers/properties // from user num1 and num2 prompt.get( ["num1", "num2"], function (err, res) { // To handle any error if occurred if (err) { console.log(err); } else { // By default get methods takes // input in string So parseFloat // is used to convert String // into Float var sum = parseFloat(res.num1) + parseFloat(res.num2); // Print the sum console.log( "Sum of " + res.num1 + " and " + res.num2 + " is " + sum ); } } ); } // Calling add function add(); Step to run the application: Run the app.js file using the following command. node app.jsOutput: Now enter the user input and view output in the console. outputSo this is how you can add two numbers in the console using nodeJs. Prompts help us to take input from users. It also supports validation and defaults over the input. Comment More infoAdvertise with us Next Article How to Add Two Numbers in Console using Node.js ? D devrajkumar1903 Follow Improve Article Tags : Web Technologies Node.js NodeJS-Questions Similar Reads How to add range in the Collection of Mongodb using Node.js ? Mongoose.module is one of the most powerful external module of the node.js.Mongoose is a MongoDB ODM i.e (Object database Modelling) that used to translate the code and its representation from MongoDB to the Node.js server.Mongoose module provides several functions in order to manipulate the documen 2 min read How to Use Complex Numbers in math.js? Complex numbers are essential in various fields of science and engineering, particularly in signal processing, quantum mechanics, and control theory. A complex number consists of a real part and an imaginary part, typically written in the form a+bi, where i is the imaginary unit. Math.js, a powerful 3 min read How to Connect to a MongoDB Database Using Node.js MongoDB is a NoSQL database used to store large amounts of data without any traditional relational database table. To connect to a MongoDB database using NodeJS we use the MongoDB library "mongoose". Steps to Connect to a MongoDB Database Using NodeJSStep 1: Create a NodeJS App: First create a NodeJ 4 min read How to Add Data in JSON File using Node.js ? JSON stands for Javascript Object Notation. It is one of the easiest ways to exchange information between applications and is generally used by websites/APIs to communicate. For getting started with Node.js, refer this article.Prerequisites:NPM NodeApproachTo add data in JSON file using the node js 4 min read How to count total number of documents in MongoDB Collection using Node.js ? MongoDB, the most popular NoSQL database, we can count the number of documents in MongoDB Collection using the MongoDB collection.countDocuments() function. The mongodb module is used for connecting the MongoDB database as well as used for manipulating the collections and databases in MongoDB. Inst 1 min read How to Use math.js in NodeJS And Browser? Math.js can be used to perform complex mathematical computations in both NodeJS and browser environments. It is a powerful library that supports a wide range of mathematical functions, including algebra, calculus, and statistical operations. By integrating math.js into your projects, you can handle 3 min read How to add new functionalities to a module in Node.js ? Node.js is an open-source and cross-platform runtime environment built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. You need to recollect that NodeJS isnât a framework, and itâs not a programming language. In this article, we will discuss how to add new functi 3 min read How to write code using module.exports in Node.js ? module is a discrete program, contained in a single file in Node.js. They are tied to files with one module per file. module.exports is an object that the current module returns when it is "required" in another program or module. We are going to see a simple code like the calculator to learn how to 3 min read How to create a Number object using JavaScript ? In this article, we will discuss how to create a Number object using JavaScript. A number object is used to represent integers, decimal or float point numbers, and many more. The primitive wrapper object Number is used to represent and handle numbers. examples: 20, 0.25. We generally don't need to w 2 min read How to Handle Units and Conversions in math.js? Math.js is a great JavaScript library that makes it easy to work with units and unit conversions. It helps you define units, change between different units, and do math with them without any difficulties. In this article, we'll learn how to manage units and do conversions using Math.js.Run the below 2 min read Like