Sum of Two Numbers in JavaScript
Add two numbers in JavaScript and print the sum. Learn why "10" + "20" gives 1020 and how Number() and parseInt() differ, with explanation and output.
1// Adding two numbers2let num1 = 10;3let num2 = 20;4let sum = num1 + num2;56console.log("Sum of", num1, "and", num2, "is:", sum);78// Using template literal9console.log(`Sum of ${num1} and ${num2} is: ${sum}`);
Sample Output
Sum of 10 and 20 is: 30 Sum of 10 and 20 is: 30
In this one
- How + adds numbers and joins strings
- How to convert text to numbers with Number()
- The difference between Number() and parseInt()
- Two ways to print a result
How it works
- 1
num1 is set to 10 and num2 to 20.
- 2
num1 + num2 is calculated and stored in sum.
- 3
console.log prints the sentence using separate values.
- 4
A template literal prints the same sentence again.
Edge cases
String input
"10" + "20" returns "1020".
Values from prompt or input fields are strings. Convert them with Number() before adding.
Units in the text
Number("10px") is NaN but parseInt("10px") is 10.
parseInt stops at the first non-digit. Number rejects the whole string. Choose based on how strict you want to be.
Decimals
0.1 + 0.2 prints 0.30000000000000004.
Computers store decimals in binary, so tiny errors appear. Use toFixed(2) when you display money.
What's going on
This program adds 10 and 20 and prints the sum. It is the simplest example of doing math with variables.
Two variables, num1 and num2, hold the numbers. The line let sum = num1 + num2 adds them and stores 30 in sum. The result is printed twice. The first print passes several values to console.log, which adds spaces between them. The second uses a template literal, a string in backticks where ${sum} is replaced by the value.
The + sign has two jobs in JavaScript. With numbers it adds. With strings it joins text. If the values came from a form or prompt(), they are strings. Then "10" + "20" gives "1020", not 30. That is the most common bug in a beginner calculator.
Fix it by converting first. Number("10") + Number("20") is 30. You can also use parseInt. The two behave differently on messy input. parseInt("10px") reads the digits and returns 10. Number("10px") returns NaN. NaN means "Not a Number", a special value for failed math. Pick the one that matches what you want on purpose.
Think of a canteen bill. A samosa costs 10 and tea costs 20. The bill must say 30. If it says 1020, you forgot to convert.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What is "5" + "5" in JavaScript?
Pick an answer to check it.
Question 2
What does parseInt("42kg") return?
Pick an answer to check it.
Questions people ask
Why do I see 1020 instead of 30?
At least one value is a string, so + joined the text. Convert both values with Number().
What is NaN?
NaN means Not a Number. You get it when a conversion or calculation fails, such as Number("abc").
Is parseFloat different from parseInt?
Yes. parseFloat keeps the decimal part. parseInt("7.5") is 7, parseFloat("7.5") is 7.5.