JavaScript for Beginners #12 - For Loops
Skills:
LLM Foundations80%
Key Takeaways
This video tutorial covers the basics of for loops in JavaScript, including syntax, examples, and use cases, providing a foundational understanding of loops and arrays for beginners in JavaScript and LLMs.
Full Transcript
hello everybody and welcome back to their JavaScript tutorial so in this video we are gonna be talking about four loops now four loops are the most common type of loops I would say almost one of the most common types of expressions that you will see in code they're responsible for millions upon millions of different tasks they are fundamental so you really need to make sure you understand the before moving forward into some more complex topics in the language so what is a for well a for loop is used when you want to loop a defined amount of times or you know how many times you want to loop so in the previous video we talked about while loops where we looped based on a condition now that condition could change at any point in time it the user could change and it can change from different areas of the code and essentially we don't know how many times were looping with that condition because it's just what that condition is true or whether it's false whereas here in our for loop we actually usually know exactly how many times we're gonna be going through the loop and I'm gonna talk about that now and show you how this works so the basic syntax for a for loop is for you're gonna define a variable in here I'm just gonna type it out and we'll talk about it later we're gonna put a condition here and we're gonna put an increment step here now I just want to show you guys how this works and then I'll explain the entire thing so just maybe you cannot use some intuition and get an idea of what I'm doing here but I'm just a for loop I've set var I equals 0 I is less than 10 I plus plus console dot log I I know this is confusing but let's have a look at this here well we get 0 through 9 prints to edge of the screen ok so what the heck did I do in how does this work well the first step of a for loop is to define a variable in this case I'm gonna call it I you can call whatever you want and set it equal to an initial value this initial value is gonna be the value that you start looping at next we type a condition now this condition is the condition that will be used to run the for loop it must involve the variable here that you use so I have to use I here and then we have an increment so I plus plus and this tells me how much to add to I every time one of these loops successfully completes inside of these curly braces it's what's gonna happen whenever we loop we could of course do multiple things in here like console dot log Tim right and then if we go here and we refresh we can get Tim every other time after all these words but that is the basics to how this works now what I can do is manipulate these numbers so that I can loop a different amount of times or through different numbers so for example rather than adding 1 each time I could add 2 so this is how much you're adding to I every loop and now watch what happens when I have my numbers I go from 0 to 8 but not to 10 because obviously I was less than 10 I could start at a different value I could start at value 5 so now we will get 5 7 9 4 anything out because we want 5 and then we added 2 each time and that is how a for loop works so you just start at some value you're less than some value and then you have I plus equals you know what I'm now in here we can do all kinds of different things I can also break out of this loop if I want so I can say something like if I'm equals equals 7 I could break and now what we'll get is all right we should get at least is just 5 7 printing out and we do because we broke one I equals 7 and this break keyword works the same as it does for a while loop it simply breaks out of whatever closest loop we find which is this for loop and yeah so now what I'm going to do is show you as an example of how we can loop through an array in here and actually see if a specific value exists in the array so what I'm actually going to do is I'm going to create a function what I want this function to do is to look in an array for a certain value and return whether we found that value or not so this will be a little bit of review from functions in case we forgot about those before so I'm gonna say function find in alright like this I'm gonna say ARR which stands for my array and I'm gonna stay value which stands for what we're looking for now what I'm gonna do is write some piece of code it's gonna look through the array and tell me if a value exists in it so what I'm gonna do is make use a for loop actually and you guys will see how this works here I'm gonna say for int I equals 0 or sorry not into I I'm coding in Java right now say var I equals 0 we'll say I is less than AR r dot length and ARR is gonna be in a rack and then we'll say I plus plus now what I'm gonna do in here is I'm going to say if ARR I equals equals value return true like that okay if I get spelled true correctly now at the end of this loop I'm gonna return false and we're gonna run through how this works okay so what have I done here well I've essentially created for loop that's gonna look through every element in the array that I pass in because it's gonna go from zero to whatever the length of the array is and we're gonna check some value every time inside of this loop so what I'm gonna do is say if the value at array whatever index I'm at is equal to the value we're looking for return true because that means we found the value so we can simply return true and by doing this that will automatically break out of this for loop so we won't continue to loop anymore we'll just return to wherever we were called from now let's say we loop through all the elements in the array and we don't find the element well we'll step out of that loop we'll start moving down will read this return false and then we'll simply return false because that means we did not find the element we were looking for so let's actually call this function and see if this works so what I'm gonna do is I'm gonna say bar arr equals and in this case we'll say one two five six seven maybe we'll throw a Tim in there we could throw a true why not now what I'm gonna do is simply say console dot log and we'll do the name of the function here so find an array and then what I'm gonna do is pass the array and what is the value that I want to look for it well actually let's look if five exists in the array so the output here should be a true or it should be a false and let's have a look at our console here we refresh we get true as we did find five in your rank now what if I do Tim three is that in the array well let's look here no we get the value false so that's just a very basic example of what you can do with a for loop it's very powerful you can do lots of different things now I will show you some embedded for loops and I'm gonna talk about four of either in this video or the next one but let's leave that function up here for now and what I want to do is I actually want to make a nut function and I'm gonna call it um mmm N squared let's like this okay and swing now in here what I'm gonna do is take some value and and what I want this function to do is print out the value n but squared so whatever n is I want to print out that like amount of x squared now I know this is confusing but you guys will understand what I mean in a second so what I'm going to do is I'm going to set up a for loop and inside here I'm actually gonna say var I equals 0 I is less than and in this case N and then we'll say I plus plus now what I'm gonna do in here is make another loop and I'm going to say for VAR j equals 0 and i'm gonna say in this case we'll say J is less than and and then J plus plus and then what I'm gonna do in here is say console dot log and will simply print I don't know it'll just print run okay so what I wanted this to do was simply have something printing out to the screen N squared amount of times now how do we do that so this is n we want to print some value N squared amount of times well what I could have done is the exponent so I could have done and and then to the exponent 2 which I think I showed in previous videos how to do but I want to explain why this will actually print the value run N squared amount of times and for those of you that are unfamiliar N squared is simply n times n right so it's n times n 2 times which is that okay so how does this work well we have this first for loop that runs from I equals 0 to n which means it's gonna run actually n times I know people like well why do you have it less than n well because since we start at 0 we go up to end but don't include N so that will actually be an amount of times that we will loop and then inside of this for loop we have another for loop that starts J equals 0 and goes J less than n which means this is gonna run end times so if this is running n times and this is running n times and this runs every time one of these runs well this actually gives us the value of N squared because what's going to happen is this is going to be on loop 0 this is gonna run already end times right then this is gonna be on and what is it I equals one so now we're gonna have n plus N and then plus N and then plus N and we're gonna keep doing that n times right and that's how many times this is gonna loop and if you're confused by this just ignore it it's not that big of a deal but I just want to show you guys some different things that we could use so now what I'm gonna do is N squared I'm just gonna put 5 in there now you should know that that value is 25 so 5 times 5 let's refresh this you can see that it printed 25 or run 25 times now if I put a value like 3 in here obviously we should get 9 and you can see we're getting 9 there so these are my kind of two examples with 4 loops I'm actually gonna stick the or stay with the 4 of loops for the next video so we'll talk about those there but hopefully this gives you an idea how you can use for loops again you have a variable you can start it wherever you want you have a condition this is what you're gonna loop until and then you have an increment so what you're adding to this counter variable here you can obviously put four loops inside of 4 loops if you want to that's what we've done here and you know a really great way to loop through elements is to use a for loop and if you want to check if something exists then you could do something like this check it against a value and you know put it in a function and return true all of that fun stuff so maybe that has been it if you guys enjoyed it make sure you leave a like subscribe to the channel and I will see you guys in the next JavaScript tutorial
Original Description
In this beginner javascript tutorial I will be discussing for loops. For loops are one of the most commonly used block of code in javascript and are extremly important to understand. They are commonly used to loop through structures like arrays and to increment a counter. I will show examples of both in this video.
Playlist: https://www.youtube.com/watch?v=ykoxwrm0Seo&list=PLzMcBGfZo4-njtc5xy3qli4cN2zlKsoxd
◾◾◾◾◾
💻 Enroll in The Fundamentals of Programming w/ Python
https://tech-with-tim.teachable.com/p...
📸 Instagram: https://www.instagram.com/tech_with_tim
🌎 Website https://techwithtim.net
📱 Twitter: https://twitter.com/TechWithTimm
⭐ Discord: https://discord.gg/pr2k55t
📝 LinkedIn: https://www.linkedin.com/in/tim-rusci...
📂 GitHub: https://github.com/techwithtim
🔊 Podcast: https://anchor.fm/tech-with-tim
💵 One-Time Donations: https://www.paypal.com/donate/?token=...
💰 Patreon: https://www.patreon.com/techwithtim
◾◾◾◾◾◾
⚡ Please leave a LIKE and SUBSCRIBE for more content! ⚡
Tags:
- Tech With Tim
- JavaScript Tutorials
- JavaScript for Beginners
- Beginner JavaScript
- For Loops JavaScript
- JavaScript for Loops
- For Loop Tutorial JavaScript
#JavaScript #JS
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
More on: LLM Foundations
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Building LSTMs with PyTorch and Lightning AI Part 7: Resuming Training with Checkpoints
Dev.to · Rijul Rajesh
How AI Learns with Less Labeled Data
Medium · AI
Comparing Sarvam-30B and Qwen2.5–14B on Spider Text-to-SQL: An Active-Parameter Perspective
Medium · LLM
Claude Sonnet 5 closes the gap to Opus without the Opus bill
Medium · LLM
🎓
Tutor Explanation
DeepCamp AI