➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
How To Add Two Numbers In JavaScript | Throughout this tutorial, we’ll look at ways to do algebraic calculations in JavaScript source code, find the addition of two numbers, and make use of built-in alert and log boxes to show the user the outcome.
In JavaScript, operators are used to determine the outcome of mathematical operations including addition, subtraction, multiplication, and division. Such operators are typically used by programmers to carry out calculations and solve additional complex math problems. Programmers employ the + operator to find the sum.
JavaScript’s addition (+) Operator:- The + operator in JavaScript returns the total of all numerical values. This + operator performs string fusion when used with string values.
Program to Add Two Numbers In JavaScript
value1 = 1
value2 = 5
sum = value1 + value2
console.log("The sum is " + sum);
Output:-
The sum is 6
How To Add Two String Numbers In JavaScript
In the below program to add two string numbers in JavaScript first, we have to convert the string into a number using the parseInt() method. Later we can add the resultant numbers. Let us see the program for it:-
var x = '50';
var y = '60';
var num1 = parseInt(x);
console.log('First number: ' + num1)
var num2 = parseInt(y);
console.log('Second number: ' + num2)
var sum = num1 + num2;
console.log('The sum is: ' + sum);
Output:-
First number: 50
Second number: 60
The sum is: 110
Function To Add Two Numbers In JavaScript
Let us see how to add two numbers in JavaScript using function. The function is a code snippet that can be used multiple times.
function sum(num1, num2) {
return num1 + num2
}
value1 = 10
value2 = 15
sum = sum(value1, value2)
console.log("The sum is " + sum);
Output:-
The sum is 25
In the below program we have taken two string numbers and converted them into integer values. Later we have called sum() function to find the addition value of the given numbers.
function sum(num1, num2) {
return num1 + num2
}
var x = '55';
var y = '16';
var num1 = parseInt(x);
console.log('First number: ' + num1)
var num2 = parseInt(y);
console.log('Second number: ' + num2)
var sum = sum(num1, num2);
console.log('The sum is: ' + sum);
Output:-
First number: 55
Second number: 16
The sum is: 71
How To Add Two Numbers In JavaScript Using TextBox
To accomplish the arithmetic operations, we necessitate a number of assignments or numbers that the user has provided. In the scenario below, we’ll take two values from the user’s request, add them, and thereafter show the user the outcome.
var x = window.prompt("Enter first number: ");
var y = window.prompt("Enter second number: ");
var num1 = parseInt(x);
console.log('First number: ' + num1)
var num2 = parseInt(y);
console.log('Second number: ' + num2)
// Calculate the Sum
var sum = num1 + num2;
console.log('The sum is: ' + sum);
Output:-
Enter first number: 10
Enter second number: 25
The sum is: 35
Enter first number: 100
Enter second number: 550
The sum is: 650
This example accepts user input. The window.prompt() here creates a pop-up box to receive input in the browser. The format of this input is a string. In order to turn a string value into an integer, we now use the parseInt() function.
Finally, add both integers together and display the results. The outcomes will be displayed in a popup box this time. This brings us to the end of the article, we hope you understood the basics of the addition of two numbers in JavaScript.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!