2

I've got a for loop like this:

for (var i=first; i<=last; i++)
{
    $("#markers").append("<div class='marker'>"+i+"</div>");
}

first is set to 2001 and last is 2010. This works fine. The problem is when I change it to:

for (var i=first; i<=last; i+=1)
{
     $("#markers").append("<div class='marker'>"+i+"</div>");
}

(Notice the different final declaration is different). Any variation other than i++ results in an infinite loop. It's very strange as a jsFiddle with the same parameters works happily. Any suggestions?

3
  • it seem to work under Chrome. Wich browser have you tried? Commented Nov 21, 2011 at 18:10
  • 2
    Curious: why would you do i+=1 instead of i++ in the first place? Seems like it's more work than it needs to be. Commented Nov 21, 2011 at 18:14
  • I actually needed to do i+=some_variable, I'd just simplified it down for the question. Commented Nov 21, 2011 at 20:17

1 Answer 1

13

I would guess that first is set in a way that ambiguously could be interpreted as a string. So the first version can only be interpreted as increment, but the second is being interpreted by javascript as string concatenation.

Sign up to request clarification or add additional context in comments.

3 Comments

Does that mean this is the answer? :-)
It did however I wasn't in front of the computer after the 10 minute limit expired. Not my first rodeo. Thanks for the great answer though!
Thanks. I knew that from your reputation. I was just enjoying my first good answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.