Saurabh

Learn JavaScript by top digital marketing companies in Pune

Overview

Another neat feature about six functions is you can give them default values which is pretty cool. So you know I'm going to this code and let's say we have some kind of function you know what kind of do our usual do. Kind of something that multiplies numbers. And normally the way you would check for default would be something like this. So if I want to know about top digital marketing companies in Pune the traditional way so function number multiplier takes into values x and the function can return x times what. So what happens if we try to invoke this function number multiplier. And we only give it one number. Let's see what happens not a number. Right.

That's what any instance for since was not a number. And it's basically top digital marketing companies in Pune picking up on the fact that we only passed in one number. If we pass into it should work. There we go. We're only passing in one. So the old way is we could check that value if that value doesn't have a value associated with it. In other words, if it's undefined we can just give it a value. So that would look something like this we'd be seeing something like X equals X or whatever number we wanted to have by default we can do the same for yy is equal to Y but if y is undefined we assign a different value for example. Right. And then we can return X times Y here even if we don't give it a value we should still see a result right.

Applications of JavaScript

It still gets the values from this way of digital media agency basically assigning the defaults on the other hand if we do pass a value. So, for example, two and three around run it right. In that case, what's valued on the left-hand side evaluates to true right. This exists, therefore, it doesn't bother going to the other side of the OR statement. And this is essentially how it used to be top digital marketing companies in Pune in terms of checking for the default. Right even if we only pass in one number so it goes by the order we pass it. Right. So, in this case, x is going to be equal to and is going to be equal to the right. And you know this is perfectly logical definitely works.

But yes this introduces the concept of default values and it's pretty cool. So let me get rid of this code and because we're using E we can actually just directly put it here so top digital marketing companies in Pune can say X equals, for example, y equals. And basically, this will only get triggered. You know I'm gonna get into that space and this will only get triggered if either of these is undefined. In other words, I can just call a number multiplier. It's going to check. Do I have a value for x is x undefined. If yes well better assign to be the default value then same with y if y is undefined. Or better assign the default value of. Clear.

You can run it and see that it is working as expected and of course we can pass in values to you. So let's try three and six for example right we'll run it again. In this case, it checks. Do I have a value for x is x undefined. In this case no X not undefined. We have a value for x. Same with ye is not undefined. It, therefore, assigns it a value or I should say it doesn't assign it this default. By the way these defaults. You don't have to be numbered. They can be basically any type of thing you need it to be. So if your function has to do with strings so let's write another function down below here. So function strings splatter for example.

And it's going to take in an array which I'm just going to call R and there's a built-in function in JavaScript that can split your string based on whatever parameter you give it. So let's say we want to take in an array that contains a string or actually let's just give it a string that will take the string We'll split it and we'll put each section into an array separated by commas. Right, in other words, I'm going to give it a string like this. And what I want to get back is the word one word two is very easy to do in JavaScript redskin to watch out for top digital marketing companies in Pune space to separate them out. So we take in the string we return String dot split and we tell it where we want it to split which in our case it's going to be the space between the words.

So that's our string splatter. Let's invoke it here. Cancel log string splutter. There is what we will give it. Let's see what it does. So we'll run it and looks like it worked as intended. That split off high into its own entry and there into its own entry grade these are now separate strings inside an array. If we don't give it any parameters let's just see what it does. Error Rate makes sense. I'm trying it's trying to split something that doesn't exist.

We'll give it a default value which can be how are you for example. Now we're giving it a default string. So we'll run it. How are you? Pretty cool stuff. We can also give it a variable. So if we define that top digital marketing companies in Pune somewhere in our app the Const initial string equals How are you for example. Now we just say default Hey default just uses the value that's associated with this constant. So this will have the same effect. Run it. How are you? But if we give it some values I am fine.

  • Love
  • Save
    Add a blog to Bloglovin’
    Enter the full blog address (e.g. https://www.fashionsquad.com)
    We're working on your request. This will take just a minute...