Saturday, 15 February 2014

JavaScript uninitialised variable behaviour -


I'm learning JavaScript I had to face this behavior using the initial and unreliable variables. For example,

  // is not an initial sum initialized; For (var i = 0; i <10; i ++) {sum = sum + i; } Alert (amount); Output: 90  

< Div class = "snippet-code">
  // initial var sum = 0; For (var i = 0; i <10; i ++) {sum = sum + i; } Alert (amount); Output: 45  

Can anyone explain to me what is happening here? I think 45 times in the initial variable amount, it seems like this.

Edit: Try to run the code separately.

will first return a Nayen (not number). I do not think this will warn about 90. I did not, I tried your script while in the second you explicitly inform the script that you are using numbers

  var sum = 0; For (var i = 0; i <10; i ++) {sum = sum + i; }  

No comments:

Post a Comment