Table of contents
1.
Introduction
2.
Using the + Operator
2.1.
Example
3.
Using a Function
3.1.
Example
4.
Using Arrow Function
4.1.
Example
5.
Using Addition Assignment (+=) Operator
5.1.
Example
6.
Frequently Asked Questions
6.1.
What is the easiest way to perform the addition of two numbers in JavaScript?
6.2.
When should I use a function to add two numbers?
6.3.
Can I use these methods for floating-point numbers?
7.
Conclusion
Last Updated: Jan 25, 2025
Easy

Addition of Two Numbers in JavaScript

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The addition of two numbers is one of the most basic operations in programming, and JavaScript provides simple and efficient ways to achieve this. Using JavaScript, you can add numbers by assigning values to variables and performing arithmetic operations. This operation is widely used in web development for tasks like form calculations, interactive features, or dynamic content updates.

Addition of Two Numbers in JavaScript

In this article,  we will discuss how to perform the addition of two numbers in JavaScript using different methods, such as the + operator, functions, arrow functions, and the addition assignment (+=) operator. Each approach is explained with proper examples, so even if you're new to coding, you’ll find it easy to understand.

Using the + Operator

The + operator is the simplest way to perform the addition of two numbers in JavaScript. It takes two operands, adds their values, and returns the result.

Example

// Declare two numbers
let number1 = 5;
let number2 = 10;


// Add the numbers using the + operator
let sum = number1 + number2;


// Display the result
console.log("The sum of", number1, "and", number2, "is:", sum);
You can also try this code with Online Javascript Compiler
Run Code


Output:

The sum of 5 and 10 is: 15


Explanation:

  1. Two variables, number1 and number2, store the values to be added.
     
  2. The + operator combines these values, and the result is stored in the variable sum.
     
  3. The console.log statement prints the result.
     

This method is simple and works for both integers and floating-point numbers.

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:

  1. The function addNumbers takes two parameters: num1 and num2.
     
  2. It adds the two parameters and returns the result.
     
  3. 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:

  1. The arrow function addNumbers is defined using the => syntax.
     
  2. It takes two parameters, num1 and num2, and returns their sum.
     
  3. 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:

  1. The variable total is initialized with 0.
     
  2. Each += operation adds a value to total and updates its value.
     
  3. 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. 

Live masterclass