Use an iterator to execute code a specified number of times:
Python | JavaScript |
names = ["Alice","Bob","Carol"] |
const names = ["Alice","Bob","Carol"] |
Now, you may be wondering, "if JavaScript has a for..in loop, why aren't we using it?". As it turns out, for/in of Python and for..in of JavaScript are false cognates: their names look alike but are very different in use. We'll discuss JavaScript's for..in loop shortly. Also, note how we needed to have three clauses in our for loop:
Figure 3.1 - Declaration, Condition, and Execution stages of a for loop
The declaration will either define an iterator or use an existing mutable variable. Note that it should be a mutable number!
Our condition is what we're testing. We want our loop to run while i is less than names.length. Since name...