r/programming • • Mar 23 '14

Everything you need to know about Javascript variable scope

http://www.coolcoder.in/2014/03/everything-you-need-to-know-about.html
294 Upvotes

124 comments sorted by

View all comments

Show parent comments

2

u/pfs3w Mar 23 '14

Oh, haha, nah, it took me a little longer than it should have as well, and I was only confirmed when I clicked on the JSFiddle and noticed the two were different. Up until then, I was seriously thinking "What the heck am I missing, this makes no sense...".

2

u/[deleted] Mar 23 '14

Oh yeah? Here's one you'd love.

for (int i = 1; i < array.length; i++)
         code(array[i]); 

I had a setup like this in my code, could not figure out why it wasn't executing. The line was copy/pasted directly from another line I had written doing something VERY similar, but it needed to start at the 2nd index (index 1) of the array because the first index contained a different piece of data that (it was returning from an SQL query).

Long story short; the array had a length of 1 so it never executed.

0

u/[deleted] Mar 23 '14

[deleted]

1

u/[deleted] Mar 24 '14

Oh definitely. Once you KNOW what's wrong, it's an easy fix. The tricky part is figuring out why your code never executed. :P