Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

CERN

Name: Anonymous 2012-01-27 17:14


for (var i = 0; i < 5; i++) {}


vs


var i;
for (i = 0; i < 5; i++) {}


Not just Javascript, any language.

Name: Anonymous 2012-01-27 17:17

First one because the variable will go out of scope after the loop.

Name: Anonymous 2012-01-27 17:24

Second one because it's compliant C89 code (under the assumption that var is a define or a typedef to the correct type).

Name: Anonymous 2012-01-27 17:50

>>2
unless it's javascript, of course

Name: Anonymous 2012-01-27 18:10

>>2


{
    int i;
    for (i = 0; i < 5; i++) {}
}


Now it is C89 compliant and the variable goes out of scope

Name: Anonymous 2012-01-27 18:24

The first is tighter from a program verification point of view as i's existence affects a lesser region of code. With the second one you at least know for sure there won't be any wasted instructions incrementing and decrementing the stack pointer while entering and exiting scope. I can't be sure what happens on the first (not just language, but compiler dependent) and too lazy to check, it could be equivalent emitted instructions after the compiler checks that i isn't used outside for.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List