Home Programs How to Add Two Numbers in JavaScript

How to Add Two Numbers in JavaScript

In this example we show how to add 2 numbers in JavaScript

We use the + operator to add two or more numbers.

Table of Contents

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

You may also like