4

What is the correct way to convert value of String variable to int/numeric variable? Why is bcInt still string and why does isNaN return true?

bc=localStorage.getItem('bc');
var bcInt=parseInt(bc,10);
var bcInt2=1;
console.log("bc------------>" +bc +" isNaN:" +isNaN(bc)); //isNaN returns true
console.log("bcInt------------>" +bcInt +" isNaN:" +isNaN(bcInt)); //isNaN returns true

bcInt2// isNaN returns false
5
  • 1
    If parseInt() returns a NaN, then your string doesn't actually contain a numeric representation of a value. If you had told us what the value of "bc" is, then perhaps somebody could help, but you failed to do that. That value is of course the key to the whole problem. Commented Aug 20, 2012 at 15:40
  • What is the value contained in the local storage item bc? If it is not a number (empty, got alpha characters etc...), that's what I would expect to see. Commented Aug 20, 2012 at 15:40
  • 08-20 18:41:02.880: bc------------>"1" isNaN:true Commented Aug 20, 2012 at 15:45
  • 1
    If there are double-quote characters around the 1 then it's not going to work. Commented Aug 20, 2012 at 15:47
  • Pointy got a point. I used stringify function in another place and then there was a double-quote and that was the reason! Thanks for helping me everybody. Commented Aug 20, 2012 at 17:43

3 Answers 3

9

parseInt returns a number only if you pass it a number as first character.

Examples:

parseInt( 'a', 10 ); // NaN
parseInt( 'a10', 10 ); // NaN
parseInt( '10a', 10 ); // 10
parseInt( '', 10 ); // NaN
parseInt( '10', 10 ); // 10

Also, you may take a look at the + operator if you want to get strings that are only numbers.

+'a'; // NaN
+'a10'; // NaN
+'10a'; // NaN
+''; // 0, that's tricky
+'10'; // 10

Edit: According to your comment, I've tested parseInt:

parseInt( '08-20 19:41:02.880', 10 ); // 8

You're doing something else wrong. parseInt returns everything till it's not a number. If the first isn't a number (or it doesn't find any number), it returns NaN.

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

Comments

1

enter image description here

let no = "25";

typeof no 'string'

let no2 = parseInt(no); no2 25

typeof no2 'number'

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
0

The answer is that I used localStorage.setItem('bc',JSON.stringify(bc)) and it added double quote to bc because it was in that case already a string and that's why parseInt wasn't working. Value was ""1"".

1 Comment

i have the same problem as this. still dont have a solution

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.