In this article, we will explore the process of performing addition operations in JavaScript.
JavaScript Numbers
In JavaScript, numerical values are represented using the IEEE 754 standard in a double-precision 64-bit binary format.
Methods utilized to add two numbers
Following are the methods that we can utilize to add two numbers in JavaScript:
- Utilizing the + operator
- Utilizing the function
- Utilizing the arrow function
- Utilizing the addition assignment (+=) operator
We will understand each method one by one.
Utilizing the + operator
In JavaScript, the + operator serves the purpose of performing addition on numerical values.
Demonstration:
We will employ the + operator to perform the addition of two numerical values.
Code:
let number1 = 25;
let number2 = 24;
let addition = number1 + number2;
console.log("Sum :", addition);
Output:
We can witness that the two numbers are added.
Utilizing function
We will employ a function to perform addition of numbers in JavaScript.
Demonstration:
We are going to create a function, assign it a specific name, and perform the addition of two numerical values.
Code:
function add(a, b) {
return a + b;
}
let number1 = 12;
let number2 = 24;
let sum = add(number1, number2);
console.log("Addition of two numbers is", sum);
Output:
We can observe the process of summing two numbers by employing a function that displays the result below.
Utilizing arrow function
We will create an arrow function designed to compute the sum of two numbers. This function takes in the parameters and subsequently returns their total.
Syntax of arrow function:
let addition = (c, d) => c + d
Code:
let addition = (c, d) => c + d;
let number1 = 32;
let number2 = 44;
let sum = addition(number1, number2);
console.log("Addition of two numbers is", sum);
Output:
In the following output, we can observe the process of adding two numbers through the use of an arrow function.
Utilizing addition assignment (+=) operator
We will make use of the addition assignment operator (+=). This operator combines the values of the left and right operands by performing an addition. Subsequently, it assigns the resulting sum back to the left operand.
A += 1 gives A = A + 1
Code:
let number1 = 15;
let number2 = 10;
number1 += number2;
console.log("Addition of two numbers :", number1);
Output:
The output presented here illustrates the application of the addition assignment operator, which is employed to calculate the sum of a pair of numbers.
Addition of floating-point numbers
Floating-point numbers are numerical values represented in decimal format that include decimal points, for example, 2.421.
Demonstration-1:
In this demonstration, we will perform the addition of two floating-point numbers.
Code:
var a = 2.151;
var b = 6;
var c = a + b;
console.log("Sum :", c);
In the JavaScript code presented above, "a" and "b" are two variables that hold floating-point values. The variable "c" is another variable that captures the sum of "a" and "b".
Output:
It can be observed that the result of "a" and "b" is 8.151, indicating that the decimal values are maintained.
We can employ the "toFixed" method to manage the quantity of decimal places.
Syntax:
toFixed(3);
The function toFixed is utilized, and the value 3 denotes the number of decimal places to which we aim to round the number.
Demonstration-2:
We will perform the addition of two numbers and employ the toFixed method to manage the number of decimal places.
Code:
var a = 2.151;
var b = 6;
var c = (a + b).toFixed(2);
console.log("Sum :", c);
Output:
It is evident that the decimal point remains set at 2, despite the fact that a number with 3 decimal places has been employed.
Demonstration-3:
We can achieve a more accurate result when summing two numbers, which allows us to round the values by utilizing the Math.round method.
Code:
var a = 2.6;
var b = 6;
var c = Math.round(a + b);
console.log("Sum :", c);
Output:
We can observe that the decimal point has been eliminated, resulting in a whole number.
Conclusion:
In this article, we have explored various methods for performing addition in JavaScript. We can carry out the addition of numbers by employing the + operator, through the use of a function, utilizing an arrow function, and by using the addition assignment operator (+=).