# How do I add numbers that have been inputted on JavaScript?

So, my code is allowing the user to input 2 numbers, num1 and num2. However the addition function is concatenating the numbers rather than adding them. How can I fix this please?

Here is a snippet of the function:

```
var sum= num1 + num2;
alert(`Sum of ${num1} and ${num2} is ${sum}`);
```

### 5 Answers

you need to cast strings to numbers, `+`

sign is concatenating strings, here are the examples

```
var sum= Number(num1) + Number(num2);
alert(`Sum of ${num1} and ${num2} is ${sum}`); // Sum of 1 and 2 is 3
```

or

```
var sum= +num1 + +num2;
alert(`Sum of ${num1} and ${num2} is ${sum}`); // Sum of 1 and 2 is 3
```

Narek Hakobyan
posted this

You can force it to string and use `"" + num1 + num2`

```
var sum = "" + num1 + num2;
alert('Sum of ${num1} and ${num2} is ${sum}');
```

Barakat Ali Foroz
posted this

## Have an answer?

JD