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
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).
Answer 54cd693251b8873c590022f8
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.
Answer 54e79c899113cbd64400019e
1 comments
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.
Popular free courses
- Free course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner Friendly4 Lessons - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly11 Lessons - Free course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner Friendly6 Lessons