Explanation on spelling/variable naming?


#1

I would like an explaination on why “vacationSpotIndex” variable is different then vacationSpots?? why does droping the “s” on the index variable is needed? Why couldnt the index variable be “vacationSpotsIndex” (with the “s”)?

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/learn-javascript-loops/lessons/loops/exercises/for-loop?action=lesson_resume&course_redirect=introduction-to-javascript


const vacationSpots = ['place1','place2','place3']

for (let vacationSpotIndex = 0; vacationSpotIndex < vacationSpots.length; vacationSpotIndex++) {
  console.log(vacationSpots[vacationSpotIndex]);
}

#2

Hi @csssurfer39577,

Names are arbitrary, you can give any names (almost, but not all) to a variable. The exceptions are called JavaScript reserved words which you can refer below.

Logically, when you code, a variable name should give you the idea of what the variable is used for. Whatever names you had given to the variable, in order to reassign/refer/use it, it has to be the same exact format, spelling and case-sensitive, that is the most important.


So for your question, it can be vacationSpotsIndex or vacationSpotIndex, both are correct. I could even replace the index name as just i or j or k like below:

const vacationSpotsss = ['place1','place2','place3'];

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

See how the above I added another ss for vacationSpots into vacationSpotsss and using the name i for vacationSpotIndex, but I will have to use the same name all over to refer to those variables, if not, it won’t work.


Conclusion:
It’s just the author of the lesson preference to use that name without the “s”, the code would still work if you change it this way:

const vacationSpots = ['place1','place2','place3'];

for (let vacationSpotsIndex = 0; vacationSpotsIndex < vacationSpots.length; vacationSpotsIndex++) {
  console.log(vacationSpots[vacationSpotsIndex]);
}

Different coder different style


Hope this clears your doubt. Cheers :slight_smile:


#3

While this is a perfect explanation I would just like to add that the author was likely trying to avoid confusion in his code being delivered to a very large audience. This is largely a grammar and contextual issue than anything else.

Think of this, the vacationSpots array only contains one list of vacation spots

const vacationSpots = ["Washington", "Boardwalk", "Florida"]

So referring to the individual index of vacation spots as vacation spot index (the index of a vacation spot) makes a lot of sense. Now imagine if your array was more like this:

const vacationSpots = [ ["Malibu", "Haiwaii"], ["China, Japan, Shanghai"] ]

Your vacationSpots doesn’t change much, it still makes sense, except the data it contains is now grouped together. Like different vacation plans. So vacationSpotIndex would be the individual items of the collection (individual locations) and vacationSpotsIndex would be the collections within vacationSpots.

vacationSpots // Collection of locations
vacationSpotIndex // Individual location among collection
vacationSpotsIndex // Individual index of a collection of locations among collection

This sort of thinking can have a huge impact on larger projects to avoid creating confusing relationships between data.


#5

Thanks for the detailed explanation on the grammar and contextual part @emgo_dev.

About that matter, couldn’t agree more that I have quite similar thinking too initially. Since vacationSpotIndex without the “s” refers to a single spot in the array, sounds totally logical to name it vacationSpotIndex without the “s”( point to one spot at a time), referring to first Vacation Spot at Index 0, second Vacation Spot at Index 1 … so on.


However, the more I think about it, there are two ways of looking into @csssurfer39577’s question, we can consider:

  • vacationSpotIndex (no “s”)

    • By the look of it, grammartically, we know it’s singular, pointing to one vacation spot. (the index of a vacation spot) just what you mentioned. Totally sound and correct.
  • vacationSpotsIndex (with “s”)

    • The more I look into it, it may also could be understood as (the index of vacationSpots array), just another way of looking it, vacationSpots’ Index, since apostrophe is taken out (shouldn’t be in a variable name), it could be viewed and shortened as vacationSpotsIndex. (At least, that’s how I’d understand it)

With those ideas inside my head, I finally retracted to even mention this matter because it sounds a bit confusing and at least to me, “s” or no “s” doesn’t change the outcome for the code.

I felt that for @csssurfer39577 to ask the question, might think that only without “s” will work, whereas with “s” won’t. Hence, it is essential to know that you can have the names you want (except JavaScript reserved words), and take into consideration of working in larger scale and passing code between teams of developers (how others interpret/understand your variables naming: obviously you don’t want to have some weird names/spellings other than trolling :sweat_smile:).

If it’s up to me, I would choose a simple counter like i, or a more general description like arrayIndex:

const vacationSpots = ['place1','place2','place3'];

for (let arrayIndex = 0; arrayIndex < vacationSpots.length; arrayIndex++) {
  console.log(vacationSpots[arrayIndex]);
}

Nonetheless, great share of explanations and thoughts @emgo_dev! Appreciated :smiley:


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.