Write a JavaScript program to add two numbers
Description: Store two numbers in variables and print their sum.
Hint:
- Use
letorconstfor the variables. - Use the
+operator.
JavaScript program to add two numbers
const a = 12;
const b = 8;
const sum = a + b;
console.log(`Sum of ${a} and ${b} is ${sum}`);
Output:
Tutorials Class - Output Window
Sum of 12 and 8 is 20
How it works
- The
+operator adds two numbers. - A template literal with
${ }puts the values inside the text.
Read JavaScript Operators. If the numbers come from prompt() or an input box, convert them with Number() first, otherwise "12" + "8" gives "128".
Learn more about the similar topics:
| Tutorials |
|---|
| JavaScript Variables |
| JavaScript Scope |
| JavaScript Data Types |
| JavaScript Type Conversion |
| JavaScript Operators |
| JavaScript Numbers and Math |
| Exercises & Assignments |
|---|
| Write a JavaScript program to add two numbers |
| Write a JavaScript program to swap two variables |