Profile image of angelawyman
Submitted by angelawyman
about 10 years

What does 'i' stand for in var i?

(I did try Googling this first.)

Is this just a convention, like using foo or hello world, or does it stand for something like integer, initialization, increase, increment…?

Can you use something else than ‘i’ ? I’ve seen ‘j’ but don’t understand the rules!! Thanks!

Answer 54cd52a4d3292f705000217c

1 vote

Permalink

Typically, it could stand for iterator, but then what does j and k stand for in the same situation? Same thing.

for (var i=0; i<11; i++){
    for (var j=i; j<11-i; j++){
        console.log(i,j);
    }
}

The of i and j in the above is strictly arbitrary. There is no specific meaning. We can name variables anything we wish so long as it is not a JavaScript reserved word (and not already defined elsewhere in the program).

Profile image of mtf
Submitted by mtf
about 10 years

Answer 54cd693251b8873c590022f8

1 vote

Permalink

For readability, it doesn’t hurt to give variables meaningful names so we can tell something about them as we study the code. But in production code, where size and speed are all the rage, short, simple variable names tend to be used more than verbose ones. Again, it really doesn’t matter. Whatever makes it easy to understand the code.

for (var key in obj){ ... }

Easy to understand variables in a for-in loop. If you haven’t studied objects yet, don’t worry. They will come up soon. Briefly, an object takes the form:

{
    key: value
}

Objects have properties, which are made up of key-value pairs. In our loop, using variable names that relate to what we are working with is helpful, and many of the names become a habit. key is one of my favorite, but again, it could be any name and still have the same purpose and do the same job.

for (var something in someThing){ ... }

will work as long as the object name is someThing. The key iterator is something. Notice that the spelling is the same but one has an uppercase T? Since JavaScript is case sensitive, they are two completely different names, and perfectly valid.

In time you will adopt many of your own conventions. Hopefully a lot of typical usage rubs off as well. This helps others to make sense of your code when they are reading it.

Profile image of mtf
Submitted by mtf
about 10 years

Answer 54e79c899113cbd64400019e

0 votes

Permalink

how do i solve 11/13 on for loops

Profile image of legogod135
Submitted by legogod135
almost 10 years

1 comments

Profile image of mtf
Submitted by mtf
almost 10 years

First, start by asking a new question in a thread that everyone can see. Do not post to old threads as only current subscribers can see your question. It’s also a form of thread hijacking. We can better assist you with your problem if you let the whole community see your question.