🔁 Iteration

Iterating Arrays

for, while, for-each — and how to get the index

The most common operation in coding interviews. Every language differs in how you access both value and index simultaneously.

Language:
Python
Loading...

All languages — Values only

JavaScript
const nums = [10, 20, 30, 40];

for (const n of nums) {
  console.log(n);
}
TypeScript
const nums: number[] = [10, 20, 30, 40];

for (const n of nums) {
  console.log(n);
}
Java
import java.util.*;

int[] nums = {10, 20, 30, 40};

for (int n : nums) {
    System.out.println(n);
}
Go
nums := []int{10, 20, 30, 40}

for _, n := range nums {
    fmt.Println(n)
}
C++
#include <iostream>
#include <vector>
using namespace std;

vector<int> nums = {10, 20, 30, 40};

for (int n : nums) {
    cout << n << endl;
}