texi rv texi rv - 2 months ago 6
jQuery Question

trying to subtract a value from my json but it returns nan


  • I am new to js

  • I am trying to subtract a value from my json but it returns nan.

  • can you tell me how to return as number

  • providing snippet of code below

    console.log("this.state.sportsInfo.thisYear--------->" + (this.state.sportsInfo.thisYear) - 1);


Answer

You're adding the thisYear value to the string, and then subtract one:

"a string" + 42 - 1
"a string42" - 1
Number("a string42") - 1
NaN - 1
NaN

To fix this behavior, simply put the -1 inside of the parantheses:

console.log("Value: " + (this.state.sportsInfo.thisYear - 1));