Learn functions in javascript by top digital marketing companies in Pune

Learn functions in javascript by top digital marketing companies in Pune



Overview of Functions

Now with this knowledge let's get rid of some of this code of my con. And let's rewrite our function up above to be more concise and we can really see where ESX can shine so we can, in fact, simplify a lot of this code because what are we returning we're just returning this mapping feature anyway. So that means we can kind of rewrite it like this. We can get rid of the curly brackets and we can just map over whatever was passed in getting rid of that right and this should be working out as is. Test it out. We can cancel out the result of user names passing in users. Let's see what happens. Run looks like it's working. Can we simplify this even more? Well, I think we can write because here we're just returning something again right. So get rid of these top digital marketing companies in Pune curly brackets like so and we can just get back the user name and then make that and let's test it out again. Save Ron. Hey, there we go. So a little bit confusing compared to how it was before.

But you know I'm going to put these on a separate line this we can put on the same line. Now to make it easier to see things so right. So that was what four or five lines of code we can reduce it down to just two lines of code. And you know this is you don't really need to do this if you don't want to. But as I said these top digital marketing companies in Pune makes it possible to make our code more concise. And again we can see what ESX kind of shines in the ability to do that. You don't have to use these function keywords all over the place. And implicit returns are pretty cool. It's this give it my array we return whatever the result of this is in this mapping we're returning back just user name. And again just a sanity check to make sure it's working clear. Here we go. Let's see a couple of other examples here of this type of functionality. And then I'm going to kind of move down and let's say we wanted to use a different built-in function in javascript or some sort of map we want to do a dot filter and you can use dot filter to basically return an array of whatever you need to filter.

Types of functions in Javascript

So to demonstrate that let's say numbers equal one two three four five six seven eight nine and we can filter like so. So as per survey top digital marketing companies in Pune let's assign a constant to the result of this filter function so filtered numbers equal numbers start filter right and filter is similar to map in that it takes a function and we are going to look for an in the way this works is you basically tell it what to filter out. So it would be something like this. We're going to return something return if the number is equal to for example. So this will filter out all the other numbers returned to us just number. In a new array. So if the console log that out wants to log filtered numbers let's check it out. Clear run. There we go. We have an array with the number five. Let's say we want to filter out so that in our filtered numbers we only have even numbers. In other words, the numbers that are divisible by and to do that we can use top digital marketing companies in Pune something called the modulo operator looks like this. And what this does basically says if the number divided by whatever number you want to divide by is equal to some remainder so if number percent is equal to zero. This will return to us only the even numbers.

Because otherwise if you try to divide by two you'll have a reminder as it's basically checking the remainder value. Let's check it out. Let's see what happens. Run OK it's still on the old code it seems here. Run it again. There we have our even numbers only because two divided by two has no remainder. There's no number left over three divided by two. Of course, there would be a reminder. If we wanted the opposite of this we know by now to use top digital marketing companies in Pune the not operator check data. So now we have our noneven numbers and pretty straightforward stuff. If you're not used to it dentally read up on the module. It takes a bit of getting used to but it's basically checking if there's a remainder over here you can check you know a different number for the remainder for example so if there's a remainder of let's see what it gives us it makes sense that there's a remainder of. Let's see nothing right. So none of these have a remainder of two. So it just returns to us an empty array. Other things we could do we could do kind of return just return all the numbers.

Not sure why you would need this but it would work and we know we can rewrite this to be a little bit more efficient get rid of the function keyword and then we can do a fat arrow and oh looks like we're just returning. So let's get rid of these curly brackets. You can put this on the same line. You don't have to. So I'm just going to put it on a separate line like so. You don't need the return keyword. All right. Good stuff. So let's test it out. Make sure it's still running cool. Sanity check. Let's see the odd numbers. Here Ron. All right still working. We also know that looks like top digital marketing companies in Pune which just one argument so we could drop off these parentheses. Is out of space here. We could do that and it should still work as expected. It looks like it still works. But again I kind of like the explicitness of having those there like so.