For loops

Alternative For Loops

We commonly use for loops to start at a numerical point and end at a numerical point, but they're not that rigid. In the below example we start with A, and on each loop we're adding B. Pretty cool if you ask me.

This

for (var start ="A"; start.length < 5; start = start + "B") {
    console.log(start)
}

Outputs

> A  
> AB  
> ABB  
> ABBB

Array and For Loops

An array is like a variable, except it contains multiple values, not just one. Arrays are easy to spot, because they're stuffed between square brackets []. An array is defined just like a variable and can contain all the jiggly bits you want - numbers, strings, booleans and possibly long pieces of code that provide a value like functions or more. This is a simple example of an array:

var myArray = [1,true,"tree",4,5,"sticks"];

For Loops in Javascript

This is fairly simple, we're creating a loop, something that is done over and over. The for bit tells us how long to do it for.

A for loop looks like this:

for (something; something < else; something and math) {
    stuff happens;
}

The space between the brackets is where the magic happens. Don't forget, those are semicolons separating the bits, commas won't work! Here's what they do:

Subscribe to RSS - For loops