DEV Community

Discussion on: Looping in JavaScript

Collapse
aminnairi profile image
Amin

There is also another way of looping that is not recommended in JavaScript because it is prone to errors, exceptions and huge memory consumption but is worth noting that is the recursive loop.

"use strict";

// Recursive loop 
const map = (f, [x, ...xs]) => x !== undefined ? [f(x), ...map(f, xs)] : [];

const numbers = [1, 2, 3, 4, 5];
const increment = x => x + 1;
const incrementedNumbers = map(increment, numbers);

console.log(incrementedNumbers); // [ 2, 3, 4, 5, 6 ]