Both those are examples or use with functions so we all know that we can have functions that return an array. So let's make one here function show array or return array. I should say return array and this one can just return an array like the do donuts. Chocolate candy bars. And then if we that we will see of course and I do this, by the way, more as a sanity check just to make sure everything's working as I expect it to. OK, so we digital marketing services India see that we are getting back our array here when we return that. So actually this console log statement is commented of actually.
Introduction
so now we can see that we are getting back in array. And this is essentially treated like any other array. So if we do some destructuring on it Or we can do something like we are doing X and Y so we can do donuts chocolate candy gummy bears equals the return value they don't want to return. I want to get the return value of return array like so. And basically, we're extracting out every piece from this array that's getting returned to us via the function. And we've declared them to be constants now. Pretty cool stuff. So if we canceled that let's check just at random let's check if we can grab out candy. Hey, there we go. So now we have our constant candy so pretty convenient right. We can just do this with functions. We can do it if we just give it to the normal array we can swap variables. I'm a really handy feature here. And another example let's say we want to give it an array to kind of search through. So let's say we want this function to do some kind of filter and returned to digital marketing services india us only if it finds what we tell it to look for. So since it's a function we can give it an array and we can say the search for this term and we can do
Return our DOT filter and filter it takes in a function like so. And using ESX methodology. So let's say it's looking for tea or a term and we want it to return. Let's see if t is equal to the term that we're looking for so let's say Konst result equals return array. We give it an array here and we can say chicken fish tofu. So that's the way we give it in the term. Let's say digital media agency we want to find a chicken. All right. So in case it's not clear what's happening we're giving it an array to search through and we're going to filter the filter function will basically return a new array to us with whatever matches here. In our case, we should only match one term. So this should work as expected. Let's find out cons. log our result run. Hey, there we go. So we successfully made a simple function to return to us an array it because the filter will always return an array. And because of that, we can just grab out the result we're looking for. We search for something else. Whoops, tofu for example. Run. And it finds tofu. If we give it something it doesn't understand. Well, let's find out. Undefined. Right because it wasn't able to find anything in its filter function. Maybe this should be digital marketing services india called a Food filter and sort of return or re food filter know I guess it doesn't need to be food it can be any kind of filter. So if we wanted to reuse this function of was two equals food filter let's just do more food anyway. You know what. I could go for a pizza and if we canceled that log result too we should see that it was successful. Hey, cool stuff.
Other neat things you can do with a raise is going to give you these concert logs is you can skip over certain values if you don't want them. So you can ignore some return values or some values. So let's say we have conc another array equals two numbers here. Q ten twenty fifty. Hundred thousand and thirty and let's say for whatever reason you want to skip a couple of these values. You can do that. And that basically kind of creates holes in your destructuring assignment. So let's say Konst know a No B. And then I want to skip that one. Go straight to see and skip that one as well. So let's see one two three four five six. One two three four five six D equals. I know there are. So essentially let's take a look at these cons. the log and let's do some template expressions your template literals rather. So do a first is a second B. Third C and Fourth D OK let's see what these values are. Clear. Run it again. So the first is makes sense second is. That also makes sense. Third, it's digital marketing services india not as you might expect. It's actually. Why. Because we kind of just left a blank space here. So it skipped over this and jumped over to the next value which is Gnome's see here which is. Same deal here. So we jumped over the next number and aim for the D. Now is actually equal to. Right, so this is convenient. They said if you ever do need to skip over any kind of value you do kind of need to know the order of things they're going to be in terms of you know the index. But this is definitely possible. So you know you probably haven't seen this kind of thing before it's pretty pretty uncommon to see something like this so if you do see it you know chances are it's just some kind of destructuring where they're skipping over a certain value and you know if you wanted to skip over a whole bunch of values if you make these all kind of blank spaces looks weird right. But this will work. So I said let me redo this. But the second is equal to this. Check it out here.
Makes sense. Skip skips all the way to. So instead of that, we can leave that one blank to if we wanted to and we can do no DE here instead. And what's that say identifier. Let's try that out. So here we go. And just our lynchers kind of doesn't really understand sometimes what we want but we see first hand make sense. Skip skip skip and then a thousand. So it is kind of hard on the eyes it's kind of hard to understand what's happening sometimes but this works. And if you want to skip the spaces you could do that as well. That still works. But that makes it maybe harder digital marketing services india maybe easier depends on your viewpoint I guess to read. But yeah it's kind of cool that you can ignore some values like that and other neat things you can use the rest pattern and use rest pattern and just to call back you know to recall what this looks like. Rest pattern is basically you see dot dot dot something. So if we want to see that in action we can see function say high for example takes in the first argument first our dot dot dot the rest of our eggs. And if this one we just Konsole log the tactics here.
And then we also console log the rest of args. So this is useful if you're not sure how many arguments, for example, you're going to be passing into a function. So, for example, say hi first value can be a name. Second can be a number for example. The third can be. Let's see one two three fake streets. Just get some arbitrary arguments in there. So let's see you run it right so the rest dot dot dot wrasse just puts together the rest of the arguments into an array. Here is we covered this while we're going to talk about functions but this is something that's possible. And so if we want to do this a different way let's just bug out the first arg call the rest of Arc's see it slightly differently. So there's our first arc and you want to stop cons. And this comment that out that one more time saves it all right again.So this is the first arc and then no matter how many other arguments you pass through it's always going to throw them all into an array. So if we start to add more like a number and a boolean for example doesn't matter the rest digital marketing services india operator will just basically put them all into an array and we can use that to our advantage when we are restructuring. So let me erase some of this and let's say we have Konst yet another are and this one can be I don't know dogs cats birds me get my quotation marks and they're snakes. Ants hedgehogs just off top of my head. And we want to kind of structure certain things out of this array. And then the rest of the things we want to kind of throw into its own array. So that's pretty straightforward.
Conclusion
We could do So our lynchers is kind of unhappy doesn't really understand it sometimes but let's see if our logic was sound. So we will console log dog string so log cats. Just as a sanity check here OK. Looks good. Now, what happens if we cancel that log. Other animals you don't need the dot dot dot because it already understands what you what your intention is here. But let's see. Let's check it out. Run and you can see that it put the rest of the things no matter how many things it is, in this case, it's strung put the rest of the strings into its own array. And by the way, if you did use the dot dot dot here it's kind of neat. Let's observe what happens. digital marketing services india Clear run which is kind of iterates over all of them but that's not what I intended so I'm going to do that. So that's kind of cool. And like I said it just does the rest. That's why it's called the rest. The operator takes the rest of the things in this case strings and puts them into its own array. So for example, if we didn't initialize a cat string Let me get rid of that. Let's see what our other animals looked like at that point. Save it on it again. And oh I forgot a comma. That's why it's not working. Comma run. There we go. And now other animals of course. It's just an array like any other. So you