---
title: "Write a Node.js function to find the sum of two numbers passed as arguments."  
description: "Write a Node.js function to find the sum of two numbers passed as arguments."  
author: "Steilla Mitchel"  
published: 2023-07-26  
updated: 2023-07-27  
canonical: https://www.mindstick.com/forum/159292/write-a-node-js-function-to-find-the-sum-of-two-numbers-passed-as-arguments  
category: "node.js"  
tags: ["javascript", "node js", "mern"]  
reading_time: 1 minute  

---

# Write a Node.js function to find the sum of two numbers passed as arguments.

Write a [Node.js](https://www.mindstick.com/articles/1499/upload-and-download-file-in-node-js) [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) to find the [sum](https://www.mindstick.com/forum/159332/how-can-i-use-sum-in-linq) of two numbers passed as arguments.

## Replies

### Reply by Aryan Kumar

Sure, here is a Node.js function to find the sum of two numbers passed as arguments:

JavaScript

```plaintext
function sumOfTwoNumbers(x, y) {
  // Check if the arguments are valid numbers.
  if (!Number.isInteger(x) || !Number.isInteger(y)) {
    throw new Error("Invalid arguments");
  }

  // Calculate the sum of the two numbers.
  const sum = x + y;

  // Return the sum.
  return sum;
}

// Test the function.
const sum = sumOfTwoNumbers(10, 20);
console.log(sum); // 30
```

This function first checks if the arguments are valid numbers. If the arguments are not valid numbers, the function throws an error.

The function then calculates the sum of the two numbers and returns the sum.

The function can be tested by calling it with two valid numbers. In the example above, the function is called with the arguments 10 and 20, and the sum of the two numbers is returned.


---

Original Source: https://www.mindstick.com/forum/159292/write-a-node-js-function-to-find-the-sum-of-two-numbers-passed-as-arguments

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
