1

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!

9
  • How are you checking any of these values? Have you tried console.log(year) after parsing it? Is it NaN? You do know that typeof only has "number" for numbers, right? There is no "integer" type in JavaScript. Commented Jul 26, 2018 at 15:20
  • 1
    you should use typeof Commented Jul 26, 2018 at 15:21
  • of course I'm using console.log()....and I want to be able to use Number.isInteger() and return true. Since my back-end method accepts an integer, and works when I pass it 2018 hard coded, but does not work when do try to get user input and parse it. Commented Jul 26, 2018 at 15:23
  • @LearningJS888 So again, what is year after parsing it? What precisely is stringValue? Commented Jul 26, 2018 at 15:26
  • so year after parsing it is just a 'number'. But it looks like the Number class recognizes true integers by using Number.isInteger(), hence Number.isInteger(2018) is true, but passing the already parsed value into Number.isInteger() return false...trying to understand how the raw number 2018 is different from the same number parsed from a string. Commented Jul 26, 2018 at 15:30

2 Answers 2

1

You could use the Unary plus(+) for this purpose which is less verbose and attempts to convert its operand into a number, like this:

const stringValue = '2018';
const year = +stringValue;
console.log('Is integer: ' + Number.isInteger(year))

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

Comments

0

Working as expected.

See below snippet:

function ctrl($scope){
 $scope.account = {
    "searchDocsByYear" : "2018"
 }
 console.log(Number.isInteger(Number.parseInt($scope.account.searchDocsByYear)))
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<table ng-app="" ng-controller=ctrl>
</table>

2 Comments

So I'm using angularJs and I got an <input> 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
@LearningJS888 check above.

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.