604
In this example we show how to add 2 numbers in JavaScript
We use the + operator to add two or more numbers.
Example 1
const num1 = 6; const num2 = 7; // add two numbers const sum = num1 + num2; // display the sum console.log('The sum of ' + num1 + ' and ' + num2 + ' is: ' + sum);
When you run this you will see something like this
The sum of 6 and 7 is 13
Example 2
This example takes user input
// store input numbers const num1 = parseInt(prompt('Enter the first number ')); const num2 = parseInt(prompt('Enter the second number ')); //add two numbers const sum = num1 + num2; // display the sum console.log(`The sum of ${num1} and ${num2} is ${sum}`);
When you run this you will see something like this
The sum of 10 and 2 is 12