1
var i1=3;
var i2="3";

var i3= i1+i2;
var i4= i2+i1;

So, i3 must be of type integer because of i1 and i4 must be of type string because of i2, but when I launch the code it turns out completely different. They are both equal to "33". How to sum them?

2
  • 1
    Does this answer your question? JavaScript adding a string to a number Commented Jan 23, 2020 at 19:49
  • " i3 must be of type integer because of i1" - Why do you think so? If any of the operands in op1 + op2 is a string the other operand will also be converted into a string. Commented Jan 23, 2020 at 19:50

1 Answer 1

1

Use Number() to parse values to number (integer/float)

var i1 = 3;
var i2 = "3";

var i3 = Number(i1) + Number(i2);
var i4 = Number(i2) + Number(i1);

console.log('i3', i3);

console.log('i4', i4);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.