Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
`for` loop with arrays | Loops
course content

Course Content

Introduction to TypeScript

`for` loop with arrays `for` loop with arrays

Proper array manipulation begins with loops!

You already know what arrays are, and that indexing in them starts from zero. You also know that in a for loop, we can assign any value to the variable i. For example, zero! But what if it's convenient for us to use a for loop together with an array? Let's think about it:

  • If we need to retrieve an element from the array, we use array[index];;
  • We have the variable i, which effectively replaces our index;
  • Combining the two points above, we can conclude that we can use the array[i] syntax inside a for loop to work with an array quickly. Let's look at an example:

Let's break down what's happening in the code above:

  1. We create an array of type number with 10 elements;
  2. Our goal is to inspect each element of the array and find which index it is located;
  3. Since indexing in the array starts from 0, our variable i initially has a value of 0. We also set the limit equal to the length of the array, which is 10;
  4. Knowing that i is the index of the element, we retrieve the array elements and display them on the screen using array[i];;
  5. Since i is the index of the element in the array, we immediately show with which index a particular element is located.

This way, we can interact with the array. Let's set ourselves a different task: retrieving each element located at an even index in this array:

By using i+=2, which is equivalent to i = i + 2, we retrieve every element in the array that is located at an even index. In other words, the index of the element should be divisible by 2. Such indices are 0, 2, 4, 6, and so on. Note also that with i, we can display the index of the element, as we did in the example above.

By the way, we don't need to manually count the size of the array! We can use the array property array.length. Let's take a look at an example, and at the same time, see how to iterate through the array from the end to the beginning (in case you need to traverse the array in reverse order).

In the example above, we initialized i as array.length - 1 because array.length is 10, and the last element of the array has an index of 9. Blame it all on zero-based indexing.

Additionally, you can use array.length not only for initialization but also within the condition. There are many different examples you can come up with for all of this; all you need is a bit of imagination and specific use cases. For example:

If the value of the array length minus the index is divisible by 2, we display that element. Otherwise, we display 'DELETED DATA' - that's the boss's orders.

What is the purpose of a for loop when working with arrays in TypeScript?

Select the correct answer

Everything was clear?

Section 4. Chapter 6
course content

Course Content

Introduction to TypeScript

`for` loop with arrays `for` loop with arrays

Proper array manipulation begins with loops!

You already know what arrays are, and that indexing in them starts from zero. You also know that in a for loop, we can assign any value to the variable i. For example, zero! But what if it's convenient for us to use a for loop together with an array? Let's think about it:

  • If we need to retrieve an element from the array, we use array[index];;
  • We have the variable i, which effectively replaces our index;
  • Combining the two points above, we can conclude that we can use the array[i] syntax inside a for loop to work with an array quickly. Let's look at an example:

Let's break down what's happening in the code above:

  1. We create an array of type number with 10 elements;
  2. Our goal is to inspect each element of the array and find which index it is located;
  3. Since indexing in the array starts from 0, our variable i initially has a value of 0. We also set the limit equal to the length of the array, which is 10;
  4. Knowing that i is the index of the element, we retrieve the array elements and display them on the screen using array[i];;
  5. Since i is the index of the element in the array, we immediately show with which index a particular element is located.

This way, we can interact with the array. Let's set ourselves a different task: retrieving each element located at an even index in this array:

By using i+=2, which is equivalent to i = i + 2, we retrieve every element in the array that is located at an even index. In other words, the index of the element should be divisible by 2. Such indices are 0, 2, 4, 6, and so on. Note also that with i, we can display the index of the element, as we did in the example above.

By the way, we don't need to manually count the size of the array! We can use the array property array.length. Let's take a look at an example, and at the same time, see how to iterate through the array from the end to the beginning (in case you need to traverse the array in reverse order).

In the example above, we initialized i as array.length - 1 because array.length is 10, and the last element of the array has an index of 9. Blame it all on zero-based indexing.

Additionally, you can use array.length not only for initialization but also within the condition. There are many different examples you can come up with for all of this; all you need is a bit of imagination and specific use cases. For example:

If the value of the array length minus the index is divisible by 2, we display that element. Otherwise, we display 'DELETED DATA' - that's the boss's orders.

What is the purpose of a for loop when working with arrays in TypeScript?

Select the correct answer

Everything was clear?

Section 4. Chapter 6
some-alt