JavaScript – the for Loop [2023] for beginners/dummies/SEO-helmets

A for loop, is a loop that will loop through (go through) the elements in an array or the properties of an Object.

You can use it to update arrays, count the number of elements etc

Loops can execute a block of code several times.

With arrays, it’s common(ish) to run the same code over with a different value.

Image source

screenshot source

For Loops tend to be used with arrays (I think!)

The code below the array in the screenshot below, starting with “for”, loops through the variable called “names” above it.
Once it loops through it create “name” and then “console.log(name);” will get it to print on the console.log screen

  • console.log just provides a way of testing and outputting stuff using web developer tools, inspect element etc.

The code below, will do the same as above, but this time it will add “Hello there” before each of the names:

You can also add if conditions. For example, in the code below, the loop will check if “Maria” is in the list, and if so, will print out “Maria is in my list”

You can get the loop to stop once it finds “Maria” by using the “break” keyword.

The above code will print “Ed, John and then Maria and “Maria is in my list”. It will then stop and it won’t print all the names up to “Potter”.

As well as using the for loop with arrays, you can also use it with a single variable like “age”, and add an incremented – for example “age++”

The code shown below will print out and loop 10 times. starting at 0, and then 1, 2 etc. until finally the condition age <10 is no longer met

When the age is 10, it will stop.

A web dev example

Get all the links on a page, and return the number of the links on the page.

When the loop gets to the 6th link, it is no longer less than links.length, and so the loop stops:

(you can click the image to see a lager version, the important code is :

for (i = 0; i < links.length; i++)

To print out the final link, change “<links.length” to “<= links.length” – meaning “less than or equal to links.length”

for (i = 0; i <= links.length; i++)

For In Loops

For in loops, are the same as for loops except they are shorter to write but less flexible.

For Loop Example with an Array

Work out the age of people, given an array with their year of birth in it.

start with the array and then a new array, which will hold the ages:

const years = [1991, 2007, 1969, 2020]
const ages = [];

for(let I = 0; I <years.length; i++) {

ages.push (2023 – years [i] );

}

console.log(ages);

the “ages.push” code, will psh the results into the ages array

Continue & Break Statements

Continue is to exit the current iteration and move to the next one

Break will exit completely.

You can use with an if statement

continue loop statement

If the type of the current element, is not a string, then continue.

This will skip any elements/values that are not strings. Continue will exit the current iteration and not print to the console.log

Using break, will terminate the loop completely.

break loop statement

Javascript Arrays for Beginners (2023)

Arrays hold values.

An array is an ordered list of values.

Arrays are 0-based, in that the first value is 0, not 1 in terms of its position.

A JavaScript array has the following characteristics:

  1. First, an array can hold values of mixed types. For example, you can have an array that stores elements with the types number, string, boolean, and null.
  2. Second, the size of an array is dynamic and auto-growing. In other words, you don’t need to specify the array size up front.

source

W3schools describes an array as a “special variable, which can hold more than one value”.

An array can hold many values under a single name, and you can access the values by referring to an index number.

arrays w3schools

source

Creating & Fetching JavaScript arrays

JavaScript provides you with two ways to create an array. 

It is a common practice to declare arrays with the const keyword.

Image source

You can also use the following code to create a new array:

let values = new Array();

or

let values = [item1,item2,...]

Once you’ve created your array, you can print the values to the console.log with the code:

console.log(values);



With “values” being the name of the array.

You can call “values.length” to get the length of the array

console.log(values.length);

Again, in the above code “values” is the name of the array.

You can define and name an array, and then “push” values into it later:

The above code will add a single value of 5 to the array named “values”.

console.log(values[1]); will print the second value – 7

console.log(values[0]); will print the first value – 5

In summary – to create an array, it’s generally best to use the “const” keyword, or the “new” keyword.

Adding & Changing Stuff Within Arrays

Below, the array name is used,and the variable number to update, the first variable.

You can try it yourself on the w3 schools website.

Accessing Arrays

You access an array element by referring to the index number:

accessing arrays

You can also loop through arrays