Using a Function
Functions make your code reusable. By creating a function to add two numbers, you can call it whenever needed.
Example
// Define a function to add two numbers
function addNumbers(num1, num2) {
return num1 + num2;
}
// Call the function with values
let result = addNumbers(7, 3);
// Display the result
console.log("The sum is:", result);

You can also try this code with Online Javascript Compiler
Run Code
Output:
The sum is: 10
Explanation:
- The function addNumbers takes two parameters: num1 and num2.
- It adds the two parameters and returns the result.
- When the function is called with arguments 7 and 3, it calculates the sum and returns 10.
Functions are a great way to keep your code organized, especially when the addition logic is used multiple times in your program.
Using Arrow Function
Arrow functions are a shorter way to write functions in JavaScript. They are ideal for modern JavaScript programming.
Example
// Define an arrow function to add two numbers
const addNumbers = (num1, num2) => num1 + num2;
// Call the arrow function
let result = addNumbers(12, 8);
// Display the result
console.log("The sum is:", result);

You can also try this code with Online Javascript Compiler
Run Code
Output:
The sum is: 20
Explanation:
- The arrow function addNumbers is defined using the => syntax.
- It takes two parameters, num1 and num2, and returns their sum.
- The arrow function is called with arguments 12 and 8, producing the result 20.
Arrow functions are concise and make your code more readable.
Using Addition Assignment (+=) Operator
The addition assignment operator (+=) adds the right operand to the left operand and updates the left operand with the result.
Example
// Declare a variable with an initial value
let total = 0;
// Add values using the += operator
total += 15;
total += 10;
// Display the result
console.log("The total sum is:", total);

You can also try this code with Online Javascript Compiler
Run Code
Output:
The total sum is: 25
Explanation:
- The variable total is initialized with 0.
- Each += operation adds a value to total and updates its value.
- After adding 15 and 10, the total becomes 25.
This operator is useful when you want to accumulate a sum over multiple additions, such as in a loop.
Frequently Asked Questions
What is the easiest way to perform the addition of two numbers in JavaScript?
The simplest way is by using the + operator. For example: let sum = num1 + num2;.
When should I use a function to add two numbers?
Use a function when you need to perform the addition operation multiple times in your program to keep your code organized and reusable.
Can I use these methods for floating-point numbers?
Yes, all the methods described here work for both integers and floating-point numbers.
Conclusion
In this article, we discussed four different methods for addition of two numbers in JavaScript. The + operator is the simplest and most direct way to perform addition. Using functions allows you to create reusable and organized code. Arrow functions offer a concise and modern syntax for performing the same operation. Lastly, the addition assignment (+=) operator is particularly useful for cumulative additions.