June 2018
Beginner
288 pages
6h 31m
English
The for loop is arguably one of the most widely used constructs in C-like languages. Here’s an example of using the for loop to iterate over a list of names and print each element.
| | const names = ['Sara', 'Jake', 'Pete', 'Mark', 'Jill']; |
| | |
| | for(let i = 0; i < names.length; i++) { |
| | console.log(names[i]); |
| | } |
The output from this code is the expected listing of the names:
| | Sara |
| | Jake |
| | Pete |
| | Mark |
| | Jill |
That loop is very familiar but far from being simple—it has way too many moving parts. We first had to initialize the variable i, then set its upper bound, pause to grimace, wonder if it should be < or <=, then decide between pre-increment and post-increment. The assault ...
Read now
Unlock full access