- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript (+) sign concatenates instead of giving sum?
The + sign concatenates because you haven’t used parseInt(). The values from the textbox are string values, therefore you need to use parseInt() to parse the value.
After selecting the value from text box, you need to parse that value. Following is the code −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initialscale=1.0"> <title>Document</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> </head> <body> FirstNumber: <input type="num" class="num1" placeholder=""><br> SecondNumber:<input type="num" class="num2" placeholder=""><br><br> <input type="button" onclick="sumOfTwoNumbers()" value="Addition"> <p class="output"></p> <script> function sumOfTwoNumbers(){ var num1 = document.querySelector(".num1").value; var num2 = document.querySelector(".num2").value; var addition = parseInt(num1)+parseInt(num2); document.querySelector(".output").innerHTML = "The addition of two numbers= " + addition; } </script> </body> </html>
To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.
Output
This will produce the following output −
Now, enter the value in both the textboxes and click the “Addition” button to get the sum −
- Related Articles
- Subtracting two numbers without using the (-) sign JavaScript
- Use the sign of $>,
- How to force JavaScript to do math instead of putting two strings together?
- Fire a JavaScript function when a user finishes typing instead of on key up?
- Fix Problem with .sort() method in JavaScript, two arrays sort instead of only one
- Why do we use a plus sign in front of function name in JavaScript?
- What is the operator that concatenates two or more string objects in C#?
- Java program to display Astrological sign or Zodiac sign for given date of birth
- Difference between sum of square and square of sum in JavaScript
- Sum of consecutive numbers in JavaScript
- Largest sum of subarrays in JavaScript
- Absolute sum of array elements - JavaScript
- Find the Sum of fractions - JavaScript
- Finding lunar sum of Numbers - JavaScript
- Cumulative sum of elements in JavaScript

Advertisements