Monday, July 15, 2024
HomeLanguagesJavascriptJavaScript Program to Add Two Numbers

JavaScript Program to Add Two Numbers

In this article, we are going to learn how to add Two Numbers in JavaScript. Adding two numbers in JavaScript involves performing arithmetic addition on numeric values, resulting in their sum.

There are several methods that can be used to Add Two Numbers in JavaScript, which are listed below:

  • Using + Operator
  • Using function
  • Using Arrow function
  • Using Addition Assignment (+=) operator

We will explore all the above methods along with their basic implementation with the help of examples.

JavaScript Program to Add Two Numbers using + Operator

In this approach we add two numbers in JavaScript involves using the + operator to perform arithmetic addition on numeric variables, resulting in their sum.

Syntax:

x + y;

Example: In this example, we are adding two numeric values by using the + operator.

Javascript




let num1 = 10;
let num2 = 10;
let sum = num1 + num2;
console.log("Sum :", sum);


Output

Sum : 20

JavaScript Program to Add Two Numbers using function

In this approach, we are adding two numbers using a function in JavaScript involves defining a custom function that takes two parameters, adds them, and returns the result.

Syntax:

function additionFunction(a, b) {
return a + b;
}

Example: In this example we are using the above-explained approach.

Javascript




function additionFunction(a, b) {
    return a + b;
}
  
let num1 = 5;
let num2 = 10;
let sum = additionFunction(num1, num2);
console.log("Sum of given numbers is :", sum);


Output

Sum of given numbers is : 15

JavaScript Program to Add Two Numbers using Arrow function

Adding two numbers using an arrow function in JavaScript involves creating a concise function syntax that adds parameters and returns the sum.

Syntax:

let addition = (a, b) => a + b;

Example: In this example we are using arrow function to add two numbers.

Javascript




let addition = (a, b) => a + b;
  
let num1 = 25;
let num2 = 25;
let sum = addition(num1, num2);
console.log("Sum of given numbers is :", sum);


Output

Sum of given numbers is : 50

JavaScript Program to Add Two Numbers using Addition Assignment (+=) Operator

In this approach we use Addition Assignment (+=) operator in which operator Sums up left and right operand values and then assigns the result to the left operand.

Syntax:

Y += 1 gives Y = Y + 1 

Example: In this example we are using the above-explained approach.

Javascript




let num1 = 15;
let num2 = 10;
  
// Equivalent to num1 = num1 + num2
num1 += num2;
console.log("Sum of the given number is :", num1);


Output

Sum of the given number is : 25

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments