Languages

JavaScript - do loops check the array.length every time when comparing i to array.length?

0 points
Asked by:
Frida-Timms
667

Do normal loops check the array.length each time?

For example a simple for loop:

1 answer
0 points
Answered by:
Admin Dirask Community
4380

Yes, the length property of an array is checked at each enumeration when the loop is constructed as follows: for (var i = 0; i < array.length; ++i) (which is a shorthand for your implementation).

Explanation

The for loop consists of three parts which are executed as follows:

  • part1 is executed before the enumeration,
  • part2 is a condition to test before each loop iteration,
  • part3 is an expression after each enumeration.

 

See also

  1. JavaScript - for statement

References

  1. for - JavaScript | MDN
0 commentsAdd comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join