for

for

PHP foreach Equivalent In JavaScript

If you are familiar with PHP you will have come across the for loop at some point. When learning JavaScript it is important to remember that it also has the ability to do the equivalent of the PHP forloop.

The following snippet shows the creation of an array of things that can't be referenced by a normal for loop due to the odd numbering of the keys.

JavaScript Simple Loop Optimisation

When writing JavaScript applications I normally write for loops like this.

for(var i = 0;i < elements.length;++i){
  // loop...
}

There isn't anything wrong with this, but it is not an efficient way of doing things. All it takes is a little knowledge of what the for loop does every time it runs. The loop can be split into three sections like this.

For Loop Debugging In JavaScript

The for loop in JavaScript can be used to iterate through all items in an array or properties of an object. This makes looping through any object or array very easy. As in the following example that printing out all items in an array.