In this article, we’ll discuss the different methods for adding two numbers in JavaScript and provide step-by-step instructions for each method. Whether you’re a beginner or an experienced programmer, you’ll find all the information you need to get started with adding numbers in JavaScript. 

JavaScript is a universal programming language used for various purposes, including adding two numbers. This simple operation can be performed using several different techniques, depending on your level of experience and the specific requirements of your project.

Method 1: Basic Arithmetic

Using basic arithmetic is the simplest way to add two numbers in JavaScript. To perform this operation, use the + operator to add the two numbers together. For example, to add the numbers 3 and 4, you would write the following code:

				
					var x = 3;
var y = 4;
var z = x + y;

				
			

In this example, the variable x is assigned the value 3, the variable y is assigned the value 4, and the variable z is assigned the result of x + y, which is 7.

Method 2: The parseInt() Function

Another standard method for adding two numbers in JavaScript is to use the parseInt() function. This function can convert a string into an integer, which can then be used in arithmetic operations. For example, to add the numbers 3 and 4, you would write the following code:

				
					var x = "3";
var y = "4";
var z = parseInt(x) + parseInt(y);

				
			

In this example, the variables x and y are both strings, but the parseInt() function is used to convert them into integers. The result of this operation is then added together, resulting in the value 7.

Method 3: The Number() Function

A third method for adding two numbers in JavaScript is to use the Number() function. This function can convert a variety of data types into a number, including strings and booleans. For example, to add the numbers 3 and 4, you would write the following code:

				
					var x = "3";
var y = "4";
var z = Number(x) + Number(y);

				
			

In this example, the variables x and y are both strings, but the Number() function is used to convert them into numbers. The result of this operation is then added together, resulting in the value 7.

Method 4: The eval() Function

The eval() function is another option for adding two numbers in JavaScript. This function can evaluate a string as a JavaScript expression, including arithmetic operations. For example, to add the numbers 3 and 4, you would write the following code:

				
					var x = "3";
var y = "4";
var z = eval("x + y");

				
			

In this example, the variables x and y are both strings, but the eval() function is used to evaluate the string “x + y” as a JavaScript expression. The result of this operation is then assigned to the variable z, resulting in the value 7.

Example with input

				
					function addNumbers(num1, num2) {
    return num1 + num2;
}

var num1 = parseInt(prompt("Enter the first number:"));
var num2 = parseInt(prompt("Enter the second number:"));

var result = addNumbers(num1, num2);

alert("The result is: " + result);

				
			

Let’s take a closer look at the code and understand how it works. The addNumbers function takes two arguments, num1 and num2, and returns their sum. The parseInt function is used to convert the input from the user into a number. The prompt function is used to display a pop-up window asking the user to enter a number. The result of the addition is then stored in the result variable and displayed using the alert function.

The JavaScript program to add two numbers is a simple program that demonstrates the basics of JavaScript. It is a great starting point for those who are new to programming and want to learn the basics of JavaScript. The programs are easy to use and can be modified to add more functionality.


Thanks for reading. Happy coding!