Now that you know how to set a default value for a specific parameter in a function, there is still a problem that you need to know. And I'm going to explain that in this tutorial. Here. In this example, we have the tax parameter as the last parameter, right? But suppose that we instead add this parameter in the second as a second parameter, and we add the quantity as the third and last parameter. And now here when we call, when we call the function total item price, we we need to set the quantity and the tax is already set as the default value.
But the quantity now is at the third place of the third parameter of this function. So how would we pass a value for the quantity parameter? If I add one here, JavaScript will assume that one refers to the tax, not the quantity And now if I save and run, I'm going to get none. Because JavaScript as I told you assume that one refers to the tax, not the quantity. So how would we solve this problem? Well, there are two ways to solve this problem.
The first one is to set a default value for the quantity. So if I set a default value one, and here I am, instead of passing one as the second parameter here, in this call, I should I should pass First, the default value of the tax and then the value of the quantity or the value of that they want to change for the quantity or the default value. But this did not completely solve our problem. Because we would have to each time we would have to pass five as as the value for the tax, even though we have we already have it. We have it as a default value and we don't intend to change it. For example, For a specific amount of time, so we don't intend to, to change it now.
So we did not completely solve the problem. So the other solution, which is much more effective is to add the parameter that is not going to be changed quite often at the end of the parameters. So at the end of the parameters list, so here, if the tax is not going to change is not going to be changed quite often. So, I would add the tax at the end here, tax equals two five, and here, I don't have I just passed the quantity if I want to change it to two, I would just add two or three or leave it as one. And I don't have here to pass the tax each time I call the function because it's already at the end. And also if for example, we have Another value that is not going to be changed quite often.
So suppose that you have a discount value discount, and you don't make, you don't make discounts quite often. So you would just leave this as zero. And here as well, you don't have to pass the discount at all. Only if you have only if you need to pass the discount, you need to first pass the tax and then pass the discount. So this would this would solve our problem problem to a good extent. Of course, it's not perfect, but this is the only way to solve such a problem is to add the value or the parameter that is not going to be changed or regularly at the end of the parameter list.
In other words, at the end of the parameters that you are going to pass to this function