do <something> N times (declarative syntax)

BreakPhreak picture BreakPhreak · Jun 12, 2012 · Viewed 133.1k times · Source

Is there a way in Javascript to write something like this easily:

[1,2,3].times do {
  something();
}

Any library that might support some similar syntax maybe?

Update: to clarify - I would like something() to be called 1,2 and 3 times respectively for each array element iteration

Answer

ahren picture ahren · Jun 12, 2012

Just use a loop:

var times = 10;
for(var i=0; i < times; i++){
    doSomething();
}