1

Why firebug said it's NaN though it's not?

When I used console.log to view the object that's got problem, it shows :

[Object { productId=1957,  amount=3,  productIndex=30}, Object { productId=2087,  amount=4,  productIndex=352}]

But when I drilled into the first object to see its detail :

amount          NaN
productId       1957
productIndex    30

which also happens with the second object too

amount          NaN
productId       2087
productIndex    352

So I tried to stringify. The result is as follows :

[{"productId":1957,"amount":3,"productIndex":30},{"productId":2087,"amount":4,"productIndex":352}]

Then I tried one last thing to ensure whether the value is not NaN :

console.log(isNaN(obj[0].amount)+','+isNaN(obj[1].amount));

which results :

false,false

Both values are definitely not NaN, but why firebug reports NaN? This also happens with Chrome too.

What could cause this? Did I miss something?

Edited : this question is simpler than those marked as duplicate I think. I did try to search for this kind of this same problem but found nothing. However, when I followed the link to the question, I'm already found an answer to this. Thanks everyone.

Solution to this : console.log is not reliable, alert() is better sometimes.

  • 4
    Almost certainly this: [*console.log of element.children shows 0 length but has three entries when expanded later*](http://stackoverflow.com/questions/38660832/). You can tell by changing the `console.log` that created your original output above to `console.log(JSON.stringify(obj));` – T.J. Crowder Nov 16 '16 at 08:42
  • @T.J.Crowder You remembered a post you answered almost 4 months ago. Wow! – Rajesh Nov 16 '16 at 08:44
  • @user1040224 you are checking NaN for first element which is object [{a:1}.{b:4}] you should infact use isNaN(obj[0].amount) which will give you proper value – kailash yogeshwar Nov 16 '16 at 08:53
  • @T.J.Crowder Thanks for your help. The log I showed on this post is what you suggested, the console.log(obj); is right above the console.log(JSON.stringify(obj)); Is there something extra I need to look at? Put setTimeout? But I never face thing like this before in coding javascript. It's wierd..?! – user1040224 Nov 16 '16 at 09:01
  • @kailashyogeshwar Sorry. I wrote it in the post, not copy it from the code. Just want to show what I did. I'd edit the post. Thanks for your notice. – user1040224 Nov 16 '16 at 09:03
  • @user1040224 you are also using "=" in your object rather than ":" anyways it works without any problem here is the link for jsbin http://jsbin.com/sozefufawa/edit?js,console – kailash yogeshwar Nov 16 '16 at 09:25
  • @kailashyogeshwar the first object log is Javascript object not JSON. – user1040224 Nov 16 '16 at 09:55

0 Answers0