Write a JavaScript program to add two numbers

Description: Store two numbers in variables and print their sum.

Hint:

  • Use let or const for 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
Interview Questions & Answers
What are the data types in JavaScript?
What is the difference between == and === in JavaScript?
What is NaN in JavaScript?
What is the difference between null and undefined?
What is scope in JavaScript?
What is the difference between var, let and const?
What is hoisting in JavaScript?