I have read and heard many times that .length
shouldn't be used in loops.
Bad:
for (i = 0; i < myArray.length; i += 1) {}
Better:
const myLength = myArray.length;
for (i = 0; i < myLength; i += 1) {}
While I understand this concern in other languages, like PHP, which uses a standalone function to count elements. Is .length
in JavaScript really calculated each time?