JavaScript tip: For Loops Shorthand | #shorts

preview_player
Показать описание
Hey dear coders 😀 I'm here with another tutorial that's about JavaScript tips again! In this short video, I'll teach you how to write JS For Loops shorter and more understandable without any complex structure. Subscribe me for more fantastic tutorials 👇

🔔 Subscribe Now!

🎵 Music
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Track: Ikson - Last Summer [Official]
Music provided by Ikson®
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Thanks for watching, I appreciate it 🙏
Made with 💗 by Hossein
Рекомендации по теме
Комментарии
Автор

the first for loop can be changed so they fit your preferences, ammount of loops, so yeah, they are just different

jesussaez
Автор

If your using the index in other meaningful ways it could be better to use a traditional for loop. It all depends on the scenario

dylanthony
Автор

For future reference I think you should research more before making a YouTube video

kyreehenry
Автор

it really depends on the case, some cases are only doable by using the for(let i; i < array.length; i++){}

monq
Автор

Colors.map((color, index) => {console.log(color, index)})

fyzz
Автор

Generally we use arrays because we are interested in the order. Using a for of loop does not guarantee that the order will be maintained. You should update this video so that people understand that. Both loops have their values, but people watching this may stop using for loops and start only using for of loops, and will run into bugs because they don't understand the way the loops differ.

jasongates
Автор

You can map through them or use a for each loop but still the c style for loop or the first one is faster

youssefmic
Автор

You've just started programming, haven't you?

MohiyuddinShaikh
Автор

It’s good for this purpose, but for(…;…;…){} allows us to use break/continue for loop interruption

uvgfqey
Автор

Sometimes you need the index in some scenarios, in my opinion, a traditional for loop is fine, it takes the same memory and everyone can read it with less than 2 weeks of experience.

basharzain
Автор

Or Do this instead:
colors.forEach((color)=> console.log(color))

samislam
Автор

Using modern functional programming: colors.forEach(c => console.log(c))

marounabiassaf
Автор

colors.map(color => console.log(color)) 😉

Hi-rvkk
Автор

there are some use cases of the above method like when you want the index of the element too

potsuaye
Автор

It really depends on use case though, while the second code looks more readable and clearner, I personally prefer the first one since it gives me more control using indexes.
Also, the C style for loop is evidently faster than the alternatives, so when iterating very large amount of data, it's the better option performance-wise (better than for-of and forEach).
Good luck with your next videos :D

mourad
Автор

I already use this method, I've always saw on stack overflow people using the other method which just confuses me and i don't understand it, the right method is better as it's more clear to what it does

VikkiVuk
Автор

Herr is what most commenters didn't talked about, it's when you need to break the loop, so you need to know exactly why you are looping and is it great if you break the loop!, only this cares when you have to improve time complexity

abdellatif_anaflous
Автор

Just because it looks cleaner doesn't mean it's better

renzralliongomez
Автор

colors.forEach(color => console.log(color))

DreamTeam-ywbg
Автор

That method is certainly more readable, however there are some cases that need the index.

Queryzi