JavaScript nested function scope issue

I came across a bizarre issue with JavaScript. I had some code generate an infinite loop in Firefox, but not in Chrome. I had some code that looked like this:

function someFunction() {
     for(i=0;i<chatIds.length;i++) {
          if(!isIn(chatIds[i], currentChatIds)) {
               startChat(chatIds[i], "join");

function isIn(value, array) {
     for(i=0;i<array.length;i++) {
         if(value == array[i])
              return true;
     return false;

The first function invokes the second function in a for loop controlled by the ‘i’ variable. In Firefox, the second function would use the value of ‘i’ from the first function and throw me into a browser crashing loop. Changing the second function’s for loop to use ‘j’ or a different variable to control the loop solved the problem.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s