Forum

    • Register
    • Login
    • Search
    • Categories

    scope of loop variables

    Scripting Forum
    2
    3
    417
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C
      CherryAnt last edited by

      What is better:
      for (var i = 0; i < 200; i++){
      or
      for (i = 0; i < 200; i++){
      and why i cant use:
      for (local i = 0; i < 200; i++){

      What is the scope of "i" on that bucles?
      Thanks

      1 Reply Last reply Reply Quote 0
      • Christoph Hart
        Christoph Hart last edited by

        I think I am following standard Javascript behaviour here which makes the scope global. There‘s no difference between for(i=0 and for(var i=0), in fact this is the only case where I allowed the parser to accept unqualified assignments of variables.

        My recommendation is to define one reg variable called i in the onInit callback and reuse it (that‘s what I am doing when I need to iterate using this loop). Bear in mind that the other for loop we discussed yesterday is much more faster and should be preferred if possible.

        C 1 Reply Last reply Reply Quote 1
        • C
          CherryAnt @Christoph Hart last edited by

          @christoph-hart thanks a lot

          1 Reply Last reply Reply Quote 0
          • First post
            Last post

          9
          Online

          855
          Users

          5.7k
          Topics

          53.0k
          Posts