DEV Community

loading...

Discussion on: Why you should stop declaring variables inside a for loop (especially in JavaScript)

Collapse
sinjai profile image
Sinjai

Since you mentioned C#, it may be worth noting that you can define a C# variable within a loop without changing the resulting IL (the code that's actually run by the VM) at all. This is very easy to test.

However, there are instances, such as when using that variable within a lambda, in which this isn't true. That said, more often than not you actually should declare your variables inside the loop in C# so they are scoped properly.