Array forEach Method in JavaScript

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back aliens my name is david reddy and let's continue with the series on javascript in this video we'll talk about array methods we have talked about arrays and then we were able to print all the values of the array with the help of for off loop but then there are some methods which we can use to achieve that okay so we'll talk about some more methods in fact we have talked about splice method push pop this time let's explore more specifically this is more for the values when you have huge amount of values and if you want to do some operations on that the famous one is the map reduce or the filter so we'll try those methods here in this example or in this video but initially we'll start with the for each method and let's see what that means see first of all when you talk about the adder in fact we don't have an array yet let's create one add it so let's say we have an array of nums of course you can go with any type of array or any name of the variable here but as of now let's go with nums and then i have few values here and let me just go with the complex values let's say 42 51 24 maybe 898 i'm just going with some random numbers 65 and 12. so you can see how many values we have we have six values here right now with these six values i just want to print all the values now yes we can do that by just printing this uh array as it is uh you can say log and you can mention nums the moment you save this we can just go back here and say run and you can see we got the array as it is okay but what i want here is i want to print each value individually because if you have that power of printing the value individually you can perform some other options as well maybe you want to multiply the number by some other number or you want to perform some other operation important thing is i want to get this value one by one you can do that with the help of for offload which we have seen before as of now let's try to use a method of an array so array which is the added numser has a method called for each so now what this for each will do is for this is a method which will give you one by one value so it will show the value it will give you the next value then you can perform the option on that so basically for each says i will give you one value at a time now it depend upon you what you want to do with that okay but then how we are going to do something with that see the thing you can do something in a programming languages with the help of methods or functions right so this for each will take one value and it will pass that value to a function now you have to accept the value in a function and you can perform any operation so basically what we are saying is we need to define a function inside a function okay so this this for each is a function of these nums and in the bracket we have to pass a function now we can go for the anonymous function oh the adder function which we have talked about so we can use that so let's use another function let's give a bracket and let's use an arrow so this function will take a value and it will perform some operation so we have to open the curly brackets for that because this is a function i just hope you remember error functions uh this is an arrow function here so what we are doing is we are trying to create a function inside the function as a parameter here i know that's tricky initially but when you try it by yourself it will make sense okay uh but what value you have to accept so this for each will give you one value at a time right so we'll accept that in one particular variable let's say n as of now i know this is not a good way of writing a variable name but that works right so let's pass n here and then this end will be passed in that function now in this function you can perform any operation with this n maybe you can print this value maybe you can multiply this by two your choice i just want to print this value here so what i will say is it's a log and let me print the value as it is i'm not doing anything else i'm just taking the value and printing it okay let's see if that works first so before the explanation and it works you can see we got all the values individually printed on the terminal so you got 42 51 24 98 65 and 12 that works okay but then how exactly is working now for each is the method of an array so it will take one value to time and that value will be assigned to n now this is a parameter or the argument you are passing now it will go to this function and you can do any operation here as you can see we are just printing it maybe you will multiply maybe you will do something else your choice but here at this point i'm just printing it and that works now you can also multiply this number by 2 if you want and that should work you can see all the values are multiplied here uh so this is how we can use a for each but again if you remember in addo function we have talked about one more thing whenever you have an add a function and if it only accepts one argument you don't need to put a round bracket for that and so you can skip that it will still work okay that's how we can use another function now apart from this or sorry that's how you can use a foliage now foliage can be used for different types of data in fact in the upcoming videos we'll talk about set we'll talk about map and you can use forage there as well okay yeah so that's about in fact you know for it also takes three parameters from not wrong you can pass the value num uh you can pass a value one value at a time you can also pass the index value and you can also pass the array as it is not tried this before let's see if that works so yeah it works so basically in an array you can pass one value which is the value itself or you can pass the value you can pass the index of that value and you can pass the entire adding uh how do we know if that works let's print it one by one let's print the number then let's print the index number as well and we'll print the nums the actual array and it should print here it is pointing the value which is 42 the first value then just printing this index value and just printing the entire ad as it is same thing goes next value 51 and then the index value and then we got the entire array itself so that's how we can use a for each method that's one of the methods of the array which we have talked about in the next video we'll talk about the filter map and reduce one of my favorite topic so see you in the next video i hope you are enjoying let me in the comment section and do subscribe for other videos bye [Music]
Info
Channel: Telusko
Views: 60,240
Rating: undefined out of 5
Keywords: telusko, navin, reddy, tutorial, java, python, blockchain, django
Id: hP0Rl5euD_o
Channel Id: undefined
Length: 6min 33sec (393 seconds)
Published: Wed Sep 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.