All you need to know about loops and methods.

All you need to know about loops and methods.

Traditional FOR LOOPS

const numbers = [10, 20, 30, 40, 50];

for (let i = 0; i < numbers.length; i++) {
  console.log(numbers[i]);
}
//Output:  10 20 30 40 50         
Article content

We all know this old fashion for-loop. I recommend not using it because there are 101 reasons to screw it up.

For-of loop,

const numbers = [10, 20, 30, 40, 50];

for (const value of numbers) {
  console.log(value);
}
//Output:  10 20 30 40 50         
Article content

this is easier to write,

but back draw is the whole loop that must be read through whether you like it or not! 😅

For each Loop

const numbers = [10, 20, 30, 40, 50];

numbers.forEach((value, index) => {
  console.log(value); //Output:  10 20 30 40 50 
  console.log(index); //Output: 0 1 2 3 4
});        
Article content

using for each loop you can get the value with the index as shown above

simple and easy to read

but you have to read the whole loop each time


these were easy...

now, the hard part starts


Methods for loops!

Deconstruction Array

let's say you have a huge array list, and you want the first, second and third values in a variable and rest on the array. We will need deconstruction of the array.

const numbers = [10, 20, 30, 40, 50];

const [first, second, third, ...restofThem] = numbers;
first;
second;
third;
restofThem;        
Article content

As we can see from the output on the sides, this is simpler than the old-fashioned declaration and assignment of each variable.

Finding items using index

Let us say you have a dictionary of names

const names = ["Alice", "Bob", "Tiff", "Bruce", "Alice"];        

so I want the index of Alice and ask the loop to search from index 1

so I want the 4 number to be returned as Alice is in the 4th index of the array

console.log(names.indexOf("Alice", 1));        
Article content

now, if we type another unknown name, it will give me a -1 use indexOf

Article content

if I want to search from end of the array use names.lastIndexOf

Article content

now, I can place them in a function, too using find or findIndex

Article content



Some, Every and Include

let us say we have an array

const digits = [10, 20, 30, 40, 50];        
console.log(numbers.includes(10));        

include function will return true if it exists,

super simple and self-explanatory.


Now, the problem is people are confused between SOME method and EVERY method

so just repeat these sentences to remember

If some of the values are true then it will return true

console.log(digits.some((num) => num > 20));        

if Every digit is matched then it is true

console.log(digits.every((num) => num > 20));        
Article content

Filter

Now, Let us say you want to copy an array to another array, but under certain criteria, let's say numbers above 20 are to be copied only.


const boundTen = digits.filter((value) => value > 20);
console.log(boundTen);        

the syntax looks like this

simple arrow function

Article content

The filter also takes arguments in its parameter

Article content

value, index and the value itself

Slice

Now, We just want to take a chunk of the array from the middle.

How we will can we do that?

we can use two filter one cuts the beginning and the other cuts the end.

Article content

but better to use the Slice method

Article content

just tell the index and it will extract it for you, SIMPLE!



To view or add a comment, sign in

More articles by Adnan Imranul Islam

Insights from the community

Others also viewed

Explore topics