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
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.