Skip to main content

For and While Statements

Loops are used to repeat a specific code logic, and both Python and JavaScript use for and while to implement loop logic.


Generate an array containing all numbers from 1 to 20, and print each item.

JavaScript implementation

let numbers = Array.from({ length: 20 }, (_, i) => (i + 1));

for (let el of numbers) {

Python implementation

numbers = range(1, 21)

for el in numbers:

Code Highlight

  • In Python, the syntax for for loop is for el in sequence:, while in JavaScript, it is recommended to use for (let el of iterable).
  • Python uses indentation to indicate the body of the loop, while JavaScript uses curly braces ({}) to indicate the body of the loop.

Difference Quick View

for loopfor (el of iterable)for el in sequence:
while loopwhile (condition)while condition:
range function-range(start, stop, step)
enumerate function-enumerate(iterable, start=0)
Getting the index in a loop

The built-in function enumerate() in Python is used to enumerate over iterable objects, and it returns a tuple containing the index and the item itself.

for index, el in enumerate(my_list):