JavaScript Program to Add Two Numbers
Last Updated :
08 May, 2024
We will explore how to add two numbers in JavaScript. Adding two numbers in JavaScript means combining numeric values together using arithmetic addition, which gives us their total sum.
There are several methods that can be used to Add Two Numbers in JavaScript, which are listed below:
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);
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);
OutputSum of given numbers is : 15
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);
OutputSum of given numbers is : 50
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);
OutputSum of the given number is : 25
Similar Reads
Javascript Program For Adding 1 To A Number Represented As Linked List Number is represented in linked list such that each digit corresponds to a node in linked list. Add 1 to it. For example 1999 is represented as (1-> 9-> 9 -> 9) and adding 1 to it should change it to (2->0->0->0) Below are the steps : Reverse given linked list. For example, 1->
5 min read
PHP Program to Add Two Numbers Given two numbers, the task is to add two numbers in PHP. It is one of the most basic arithmetic operations and a fundamental concept in programming. Examples:Input: num1 = 10, num2 = 20Output: sum = 30Input: num1 = 70, num2 = 120Output: sum = 190Table of Content Using + operatorUsing Arrays and Arr
3 min read
JavaScript Program to Add Two Binary Strings Here are the various ways to add two binary stringsUsing parseInt() and toString() The parseInt() method used here first converts the strings into the decimal. Ten of these converted decimal values are added together and by using the toString() method, we convert the sum back to the desired binary r
4 min read
JavaScript Program for Multiplication of Two Numbers In this article, we are going to learn how we can multiply two numbers using JavaScript. Multiplying the two numbers in JavaScript involves performing arithmetic addition on numeric values, resulting in their sum. JavaScript supports numeric data types. In JavaScript, both floating-point and integer
4 min read
JavaScript Program to Perform Simple Mathematical Calculation In this article, we are going to learn how perform simple mathematical calculations by using JavaScript. In this, we will get the two numbers and one operator as input and then the program will calculate the result according to the provided inputs. Example 1: Input : Operation : '+' num1 : 5 num2 :
4 min read