I got a question about parsing a string to a true integer in javascript. I have a back-end method I've written that accepts an accountNumber (string) and a year (int), and I am calling into this method from the front-end.
The method complains unless it's being passed an actual int, and not a 'number'.
I've tried parsing my string a few ways:
const year = parseInt(stringValue);
typeof year == number
const year = Number.parseInt(stringValue);
Number.isInteger(year) == false
Number.isInteger(2018) == true
I even tried using the Math class, but with no luck.
const year = Math.floor(stringValue);
Where the year is still just a 'number'
In addition, I tried hard coding 2018 into my function and it worked as expected. I just can't seem to parse my value to a true integer and am wondering, how can I go about this? Or is it just recommended that I accept a string on the back-end and parse it there?
====== EDIT =======
I am trying it with AngularJS.
So I'm using angularJs and I got an with ng-model="account.searchDocsByYear", and I use the $scope value of account.searchDocsByYear as the argument for parsing. It still returns false. Not sure what is different. All the mentioned examples check out fine for me too. But console.log(Number.isInteger(Number.parseInt($scope.account.searchDocsByYear))); still returns false
Thanks!
console.log(year)after parsing it? Is itNaN? You do know thattypeofonly has"number"for numbers, right? There is no"integer"type in JavaScript.typeofyearafter parsing it? What precisely isstringValue?