For Each loop in javascript

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I'm really thinking that this file is getting pretty long so should we create another file let's go ahead and create another one we're gonna copy/paste some code let's go ahead and create a new file let's call it as 13 and let's call it as loops part two maybe or part three is gonna come up soon so there we go and I'm gonna borrow up my state list because I loved it so we're gonna borrow that copy it and paste it up here okay before I talk about the for each loop let me tell you that for each loop is not considered as technically loop but actually as a method on the array so you're hardly gonna find it in the documentation section under the loops category it's not there but still it is a loop of very much being used and it works exactly like a loop but before that actually I want to share something interesting here we actually host out these mock tests for interviews and everything on every single week and these are 100% free no need to even buy course or anything you can take these tests and there was a question given in the JavaScript test which confused a whole lot of people let me show you with that as well so this was posted up there so we're gonna simply say war is gonna be zero it's gonna be zero and this was the syntax that was posted up so we're gonna simply see four and we see there are three parts of it first and foremost part is the incrementer so let's just call our declarations so declaration was there then separated by a comma why are you doing this separated by comma we have these checker which checks out the things are accurate or not or should I go inside or not and then we have got our increase or slash degree so this is what we have studied so far so what was Austin the question is something like these one semicolon and two semicolon and then after this was it okay interestingly we had another syntax going up here so if we simply said hey you know what we're gonna simply say if I is actually less than three we're gonna simply say break and otherwise we're gonna simply say log and we are gonna simply log the value of I and we are gonna also say I plus plus okay interestingly so what's going to be the out now if you understand the loop syntax clearly it's not really even a difficult problem and the whole tricky part is actually this last one the line number 15 which is I plus plus now it's impossible for a loop to run without having an incrementer but it's not really compulsory that you put out incrementer even in the for loop at the exact place where it is mentioned the whole idea is that whatever is taking your loop forward that value should be increased up you increase it up here that's the most recommended way but you can actually increase it in the body as well in case you want to be tricky will I be ever writing this code in the production and never 100% never this is a bad code I if I will be writing it in the production but production and preparing for interviews are two different things we might use them in some comparative programming or something but not in the production so in case you are wondering what's gonna be the output let me show you that so we're gonna simply say node 0 3 and we are in the 13 there we go pretty simple 0 1 2 3 and it makes 100% sense we're not doing anything it just is gonna skip all of this and it will directly come here and if it's gonna be greater than I it's gonna just break the execution till that it's gonna keep on increasing here and here so no big deal again just wanted to mention that yes this actually is here so and again if you want to take part in these quizzes which happens every weekend they are all free at learned code online app go ahead enjoy it in if not that's okay up to you ok moving further so now we have got these lists which is my state and I want to show you that how these are for each loop actually works again not exactly counted as loop but 100% is a loop so my states as soon as you put a dot you see all the methods like push length map all of them some of them we have discussed you're gonna see this for each as well let me start writing that so for each and notice here as soon as I say for each and I put these starting paren thisis notice here it says callback function and the advantage of this for each loop is if you remember here in the loop section if I had to loop through the values I had to use these my states and the square brackets and mark indexes this is some time pain because there's a lot that you can avoid by using the for each for each takes a callback function so how do we give a callback function we have already kind of seen it once and this is the one syntax another one is this syntax feel free to use anything you know the difference between them and for each goes through automatically with each element you don't have to say is my state square brackets I you don't say that I'm going to simply say s for state and here I'm gonna just simply say log s that's it that's all I'm saying I'm not saying the square brackets I am not using my states look how simpler this this entire thing is you simply loop through each and every object it automatically goes through that and you just print that object now let me just show you first by printing it out notice we get all of these values here so that is well the great advantage of having a for each loop you don't have to say I there is no initialization of the value there is no worrying about how much how should I calculate the length of array there is no worrying about should I increment the value or should I decrement the value there is nothing like that we already know it's an array that means the memory space is fixed and that that's already being declared so why not to have a method like this for each which can loop through any trade now surely we are using a shorthand syntax of the parentheses here we can use the curly braces return the values to whole bunch of things that you really want to do but this is what it is now shortly later on when we'll be doing maybe some projects if you'll request enough in the comment section maybe when we'll be doing some projects or to-do lists this is insanely used and I use it probably almost everywhere I don't use much of the for loop on arrays every single time if there is an array I'm gonna use for each it makes sense so that's it and that's all it is told you it's really really simple nothing to be painting about just one line of code and so easy to understand even if I remove these extra parentheses then it's gonna become much more easier so let me just show you that also so if I go ahead and remove these parentheses and I see this now this is just one-liner see how simple it is yeah my co-editor actually brings it back but we don't we don't need them but anyways this is so simple to have it let's run it one more time and we see two times being printed the values so told you it's really simple but there are a few more loops which are introduced in the JavaScript so make sure you just keep in mind that yes in the es6 and later on version of javascript we have for each for in for off loop as well and if you're going to be using something like angular they are almost everywhere so just keep that in mind that yes there are a little bit more loops available that's it for this one and let's catch up in the next one have you hit that subscribe button or probably you are not even watching this part come on I'm making so many videos I deserve at least one subscribe that's it for this one let's catch up in next one [Music]
Info
Channel: Hitesh Choudhary
Views: 16,505
Rating: 4.9741268 out of 5
Keywords: Programming, LearnCodeOnline, javascript, javascript for beginners, javascript course, javascript tutorials, learn javascript, udemy, coursera, udacity, learn js online, javascript projects, js tutorials
Id: QAFjcAxcNcY
Channel Id: undefined
Length: 7min 39sec (459 seconds)
Published: Wed Aug 05 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.