Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8375255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:07:43+00:00 2026-06-09T15:07:43+00:00

I am new to CoffeeScript and very excited about it. I made some basic

  • 0

I am new to CoffeeScript and very excited about it. I made some basic loops here. Now, CoffeeScript is defining a loop variable for every loop there is as follows:

var food, _i, _j, _len, _len1;

for (_i = 0, _len = fruits.length; _i < _len; _i++) {
  food = fruits[_i];
  console.log(food);
}

for (_j = 0, _len1 = vegetables.length; _j < _len1; _j++) {
  food = vegetables[_j];
  console.log(food);
}

I used to code my loops like this:

for(var i = 0; i < fruits.length; i++) {
    console.log(fruits[i]);
}

for(var i = 0; i < vegetables.length; i++) {
    console.log(vegetables[i]);
}

i was my loop variable for every loop (nested loops excluded). Now I learned that you should always declare your variables before defining it. So I changed my coding habits to:

var i;
for(i = 0; i < fruits.length; i++) {
    console.log(fruits[i]);
}

for(i = 0; i < vegetables.length; i++) {
    console.log(vegetables[i]);
}

As long as I am in the same scope I did not see anything wrong with it, but the compiled CoffeeScript code left me wondering.

Why would CoffeeScript use a different variable for every loop?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:07:44+00:00Added an answer on June 9, 2026 at 3:07 pm

    Without having looked at the CoffeeScript source code, here’s my (educated) guess:

    The CoffeeScript interpreter simply creates a new for loop construct for every for .. in you write. While constructing output JS, it maintains a table of local variable names and appends to that as necessary.

    After all, you could nest these for .. in loops, in which case you’d start to need separate loop variables in JS anyway.

    It would be necessary to track which variables in a local function scope could be potentially re-used. This is possible, but more complex than it’s worth – it simply does not provide any benefit, so CoffeeScript does not do it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Coffeescript, and I have a question about Ajax. jQuery -> api
I feel quite excited about HAML and CoffeeScript and am working on tutorial showing
I am new to CoffeeScript and JavaScript so I am having some trouble displaying
I am very new to coffeescript, and I have been trying to find a
I'm new to coffeescript, and while there is extensive information concerning the proper method
I'm using this coffeescript code: in a spec file: index = new MeetingIndex(render: false,
New to PHP and MySQL, have heard amazing things about this website from Leo
new here and new to jQuery. I've searched for an answer to my question/
New programmer here, I am trying to understand and break down this code below
New developer here,Im using the Custom Image Picker by ray wenderlich. But what I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.