Default Parameter

6 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$39.99
List Price:  $79.99
You save:  $40
€34.07
List Price:  €68.15
You save:  €34.08
£29.90
List Price:  £59.81
You save:  £29.91
CA$55.09
List Price:  CA$110.20
You save:  CA$55.11
A$60.11
List Price:  A$120.24
You save:  A$60.13
S$51.67
List Price:  S$103.35
You save:  S$51.68
HK$311.17
List Price:  HK$622.43
You save:  HK$311.25
CHF 31.89
List Price:  CHF 63.80
You save:  CHF 31.90
NOK kr405.39
List Price:  NOK kr810.89
You save:  NOK kr405.49
DKK kr254.40
List Price:  DKK kr508.88
You save:  DKK kr254.47
NZ$68.89
List Price:  NZ$137.80
You save:  NZ$68.91
د.إ146.86
List Price:  د.إ293.76
You save:  د.إ146.90
৳4,879.28
List Price:  ৳9,759.79
You save:  ৳4,880.50
₹3,622.47
List Price:  ₹7,245.85
You save:  ₹3,623.38
RM163.85
List Price:  RM327.74
You save:  RM163.89
₦58,088.27
List Price:  ₦116,191.07
You save:  ₦58,102.80
₨11,189.43
List Price:  ₨22,381.66
You save:  ₨11,192.23
฿1,257.46
List Price:  ฿2,515.23
You save:  ฿1,257.77
₺1,707.41
List Price:  ₺3,415.26
You save:  ₺1,707.84
B$216.72
List Price:  B$433.49
You save:  B$216.77
R674.84
List Price:  R1,349.86
You save:  R675.01
Лв66.58
List Price:  Лв133.19
You save:  Лв66.60
₩58,981.65
List Price:  ₩117,978.05
You save:  ₩58,996.40
₪128.88
List Price:  ₪257.80
You save:  ₪128.91
₱2,364
List Price:  ₱4,728.60
You save:  ₱2,364.60
¥6,229.04
List Price:  ¥12,459.64
You save:  ¥6,230.60
MX$720.37
List Price:  MX$1,440.93
You save:  MX$720.55
QR145.51
List Price:  QR291.06
You save:  QR145.55
P529.02
List Price:  P1,058.17
You save:  P529.15
KSh5,148.59
List Price:  KSh10,298.48
You save:  KSh5,149.88
E£1,901.12
List Price:  E£3,802.72
You save:  E£1,901.60
ብር6,238.54
List Price:  ብር12,478.64
You save:  ብር6,240.10
Kz36,670.83
List Price:  Kz73,350.83
You save:  Kz36,680
CLP$36,400.89
List Price:  CLP$72,810.89
You save:  CLP$36,410
CN¥282.10
List Price:  CN¥564.28
You save:  CN¥282.18
RD$2,553.36
List Price:  RD$5,107.36
You save:  RD$2,554
DA5,186.46
List Price:  DA10,374.22
You save:  DA5,187.76
FJ$90.84
List Price:  FJ$181.72
You save:  FJ$90.87
Q305.81
List Price:  Q611.69
You save:  Q305.88
GY$8,353.11
List Price:  GY$16,708.30
You save:  GY$8,355.19
ISK kr5,054.73
List Price:  ISK kr10,110.73
You save:  ISK kr5,056
DH367.32
List Price:  DH734.73
You save:  DH367.41
L674.94
List Price:  L1,350.06
You save:  L675.11
ден2,095.42
List Price:  ден4,191.37
You save:  ден2,095.95
MOP$320.16
List Price:  MOP$640.40
You save:  MOP$320.24
N$673.61
List Price:  N$1,347.40
You save:  N$673.78
C$1,469.47
List Price:  C$2,939.31
You save:  C$1,469.83
रु5,777.17
List Price:  रु11,555.79
You save:  रु5,778.62
S/134.42
List Price:  S/268.88
You save:  S/134.45
K170.32
List Price:  K340.69
You save:  K170.36
SAR150.05
List Price:  SAR300.13
You save:  SAR150.08
ZK921.31
List Price:  ZK1,842.85
You save:  ZK921.54
L173.39
List Price:  L346.82
You save:  L173.43
Kč826.55
List Price:  Kč1,653.32
You save:  Kč826.76
Ft13,116.32
List Price:  Ft26,235.92
You save:  Ft13,119.60
SEK kr370.70
List Price:  SEK kr741.49
You save:  SEK kr370.79
ARS$57,615.59
List Price:  ARS$115,245.59
You save:  ARS$57,630
Bs275.88
List Price:  Bs551.83
You save:  Bs275.95
COP$152,061.07
List Price:  COP$304,160.17
You save:  COP$152,099.10
₡19,972
List Price:  ₡39,949
You save:  ₡19,977
L1,051.16
List Price:  L2,102.60
You save:  L1,051.43
₲268,190.58
List Price:  ₲536,448.22
You save:  ₲268,257.64
$U1,566.95
List Price:  $U3,134.29
You save:  $U1,567.34
zł144.16
List Price:  zł288.36
You save:  zł144.20
Already have an account? Log In

Transcript

Sometimes you don't need to change values that you pass to a specific functions quite often. So for example, suppose that you run a store, and you have items. And each item has taxes, and the consumer that buys from you must pay taxes on each item he buys from you. And the amount of tax that you impose on your consumer will not change quite often. You just say the tax, for example, for one year, maybe six months, maybe three years, I don't know. But it doesn't need to be changed.

Quite often you just add that tax that value or keep it for a long period of time. Therefore you don't need each time to pass that value. Just set a default value and maybe after one year or that specific period of time you change this amount, according to regulations. So let's take an example. So now suppose that you run a store. And you have a function that takes the item as well as the quantity that the consumer is going to pay for.

And also it takes into account the amount of tax. So let's now create this function. So this function is called function is called Total item. Price. So this function is going to calculate the amount or the total amount that the consumer is going to pay for a specific product. So here, it's gonna take three parameters.

The first one is the item itself, the item, and the second parameter is the quantity, one TT and the third item is the tax, the amount of tax that you impose. on that product that you add to that product so that the consumer must pay for that for that project. So here, if we just create a new variable called item, or total total price, and it's going to be equal to, obviously to the item price, multiplied by the quantity, plus the amount of tax that the consumer is going to pay for. And finally, what we need to do is just return the total return the total price, return the total price, which is this value, right? And if we just call this function now, total price is total item price. And I am going to just add it into first into the console log, console dot log.

And then I'm going to pass parameters. The first parameter suppose that the product is for $100. And then the quantity the consumer, this person is going to buy only one, one item of that product, and the tax amount that you impose $5 on each or on any amount of that product that this consumer is going to pay for. So suppose the the value is five $5 on any amount, regardless of the quantity regardless of the price, just $5 each time the consumer steps his foot into your store, or each time the consumer just creates a cart on your website, so he or she is going to pay five bucks. Now if I save and run what I'm going to get is obviously 105 because one because hundred here hundred plus five equals To the total price, right. And there is nothing wrong with that.

But instead of each time that you call this function, and you're gonna call this function, believe me a lot of times, because if you have many consumers, each time that the consumer buys from you, you need to call this function. Therefore, instead of passing this value each time the tax value, what you would do instead is that you set a default value. And in order to set a default value, for that amount, or for that parameter, what you would do is you get here in the parentheses of that function definition, and type equals and then you set your value, so I want it to be five, and here, I'm going to delete it from the call, I'm going to delete and consider just consider that the function takes two parameters. And the first, the third one, the last one is just a default value. So JavaScript to We'll know that this valley is a default.

And it knows that it takes three parameters, but you pass only two parameters. Now if I save and run, what I'm going to get is the same exact result. So you don't have to each time pass the same value. If that value is some kind of a value, that will will be the same for quite a long amount of time. So just create a default value. And if you want, suppose that for some specific reason, you want to change that tax for one month only to increase that tax or decrease that tax for let's say, one month, what you would do is here inside the call, you add the amount that you want for you want to change that tax by So suppose that you want to change this tax from five to 15.

I would here at 15. And JavaScript here is is going to ignore the default value and take this value into account. Now if I save and run I am going to get 115 because JavaScript now knows that this value, this default value is no longer valid, because you have your own value that you set into the, into the call of this function. So in conclusion, default values for parameters are very useful in case now the value is not going to consistently be changed. And you want to stay away from the headache of just passing the same number or the same value over and over and over again, and only in case you want to change it. You can just add it in cases in some specific cases in which you want just to change that value for a specific amount of time or maybe for a specific item or specific consumer.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.