Solving the ‘Return the Sum of Two Numbers’ Problem in JavaScript

Introduction to functions and using basic math in JavaScript

Max N
2 min readJun 13, 2023
Photo by Mick Haupt on Unsplash

Introduction

In this tutorial we will create a method that takes two integers as arguments and returns their sum. For example, if we pass the arguments 3 and 2 to the method, it should return 5. Similarly, passing -3 and -6 should return -9, while passing 7 and 3 should return 10.

Step 1: Understand the Problem

To solve any programming problem, it is essential to fully understand the requirements. In this case, we need to create a method that takes two integers and returns their sum. Remember, an integer is a whole number without any decimal places.

Step 2: Define the Function

Let’s start by defining our JavaScript function. We’ll name it sumOfTwoNumbers to reflect the problem's description. Our function will take two parameters: num1 and num2, representing the two integers we need to sum. Here's the initial function structure:

function sumOfTwoNumbers(num1, num2) {
// Implementation goes here
}

Step 3: Add the Numbers

--

--

Max N
Max N

Written by Max N

A writer that writes about JavaScript and Python to beginners. If you find my articles helpful, feel free to follow.