I am aware thread title may not be very helpful. Hopefully this example below a description will make thing clearer. I will say I have deliberately keep the code simple I feel as naming rather than coding issue, a complicated existing program of mine is unnecessary.
colours=[ "red", "yellow", "blue"];
let colourI="";
for(let I in colours){
colourI=colours[I]
console.log(colour)
}
I have been told in another thread colourI is incorrect javaScript. I could get hung up on the use of word incorrect but I take it that in this context it is being using more casually as a synonym for bad practice or against convention. Personally I find calling variable instead colour gets confusing for me in complicated programs with using the naming convention I have prevent making these errors. I anyone else like me and if so or not suggest a suitable alternative, complaint to JavaScript conventions (or however you say it). The best I can thing of thisColour, which does not seem right.
You should use a lowercase i here: for(let I in colours){
If colours is a list of colours then a variable which contains 1 colour should be called colour (Which seems to be what you are trying to send to the console…)
Better yet use a for... of loop and you don’t need the index variable
In fairness I usually use standard for loops or forEach, hence why is not already instigative to me. I decided for the example the former maybe was not the best and avoid the other for particularly good reason just deciding to stick to simple javaScript for the example. However this is nice to remember for future.
On the I point, I was trying to maintain consistent capitalisation for example.
last line of code was a mistake I meant to write console.log(colourI) as you tell I did not test my code prior to submitting which was unwise. So there is only one way allowed write variable names, even if that way is very prone to some to cause errors. I also had thought programming was not so rigid.
Use what you like. forEach works the same as a for... of loop in that you have a variable that takes the value of each element of an array or collection.
Regarding the name of a variable that holds a string of a colour, colour is a good name. Just as colours is a good name for an array of many colours.
colourI is not a good name because it’s not descriptive and it’s also not much different than colours[i] which is much more clear. I can see it’s the colour at index i
You can do what you like but there are conventions which make things more quickly recognizable. To keep track of an index in a loop like that it’s almost always i or maybe x or j for a nested loop and you’ve already used i.
If you or other people see an i in your code it’s immediately recognizable as a loop index
Uppercase also has a convention:
Variable and function names written as camelCase
Global variables written in UPPERCASE (We don’t, but it’s quite common)
When iterating through all collection elements, avoid using the classical for (;;) loop; prefer for...of or forEach().
Do not use for (;;) — not only do you have to add an extra index, i, but you also have to track the length of the array. This can be error-prone for beginners.
Now, you’ve asked for advice about the name of a variable. This is the advice. You can really use whatever you want. But if you want advice here are the general conventions people use.