1

I am working in an angular 4 application ,In this I am trying to add 1 with the parameter value of a method .

Ex: if the method receives 1 as the parameter value I want to add parameter value + 1 inside the method .but it returns 11 instead of 2.

addCount(mCount){
   mCount += 1;
   console.log(mCount);
}

can anyone help me to solve this

1
  • 3
    of course mCount is a string then. mCount = parseInt(mCount) + 1; Commented Aug 1, 2018 at 11:14

2 Answers 2

5

You might be having your value as a string, just convert it into a number before adding. Use + operator to convert it into a number.

addCount(mCount){
   mCount = +mCount + 1;
   console.log(mCount);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks buddy @Prachi
2

use ParseInt to convert to a number, otherwise it will be considered as a string and concatenated,

mCount = parseInt(mCount) + 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.