Menu BAR

FEEL FREE TO ASK ANY PROGRAM and please mention any error if you find it

13 Nov 2022

Node JS program to create a module

2) Program to create a node module

Let us create a Calculator.js file with add, subtract, and multiply functions ->

// Addition of two numbers
exports.add = function (x, y){
    return x+y;
}

// Subtraction of two numbers
exports.subtract = function (x, y){
    return x-y;
}

// Multiply of two numbers
exports.multiply = function (x,y){
    return x*y;
}

Let us create a new node file to call these functions ->

var calc = require ('./Calculator.js')
var x = 15, y= 10;

console.log (" Addition of two numbers is: "+ calc.add (x,y));
console.log (" Subtraction of two numbers is: "+ calc.subtract (x,y));
console.log (" Multiplication of two numbers is: "+ calc.multiply (x,y));


===OUTPUT===

Addition of two numbers is: 25
Subtraction of two numbers is: 5
Multiplication of two numbers is: 150

No comments:

Post a Comment