The best way to loop over an array in Javascript #shorts

preview_player
Показать описание
I usually use the for of loop whenever possible since it works with async / await.

------------

Рекомендации по теме
Комментарии
Автор

other thoughts to consider. the former way allows you start at any index in the array.

love the content. This would be the content I would make if I wasn't too lazy to start a channel lol

IkeVictor
Автор

Just remember to use const instead of let so you don't end up changing the variable's value by accident

hadawardgz
Автор

Use forEach, map, filter, etc for declarative approach, and for loop, while for imperative approach.

laodemuhammadalfatih
Автор

Surprisingly, I prefer the longer version for certain situations. Sometimes you may want to count down rather than up. Also, JS feels better when it's verbose in some situations like using a regular function over an arrow function when declaring a function but definitely using an arrow function as a callback. Or is it just me?

fave
Автор

i use to use fof, but eslint gives a lot of warnings to iterate, and doing a small research there is a BIG difference between a classic for, and a fof, fin, for each, and more

misterl
Автор

The traditional for loop is faster than other iterating methods such as while, for..of and for..in

NoOne-evjn
Автор

In 5 years of using JS professionally, I have needed a classic for loop once. I still wouldn't really recommend using that type of foreach shown in the video, i'd rather use `array.forEach((element, index) => { doStuff });`

or if possible, reduce, some, every and similar are even better

YamiSuzume
Автор

I still do prefer the non imperative array.forEach(); and if you want to start from a point in an array just use slice() with it ( you can even do range with this setup )
[1, 2, 3, 4, 5].slice(1, 4).forEach(el => console.log(el)); // 2, 3, 4

fenilli
Автор

And if you need access to indices, you could do:

for (const [idx, number] of numbers.entries()) {}

Came from Python and it's a pretty good equivalent to enumerate()

No idea about performance though

re.liable
Автор

It's only for rendering data but for filtering data i think we need to use first method. Btw noice short cut

soulking
Автор

for...of works on every Iterable, and arrays are iterable :)

accidentalengineering
Автор

the best way is probably using forEach

pandasoli
Автор

In Python you can use *for number in numbers:*

SyberiaK
Автор

how would you reverse it? similar to doing a for (let I = array.length-1; I >= 0; i--)

channelname
Автор

I only use "for of" if I have to use "await" inside a loop, otherwise I prefer array.forEach

soniablanche
Автор

let x of xx is only good for readability. It doesn’t have the best performance if you’re processing large data.

Notoriousjunior
Автор

Video idea: Diff between for await and for...

adrisongomez
Автор

Is there a point to doing loops this way? Isn't it always better to use the array methods? Btw love the content keep on going bro

theofanisbirmpilis
Автор

This is available in most i guess dynamic languages.

ZeroSleap
Автор

The same as forEach right? And for(i in array) ??

juanpunch
visit shbcf.ru