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
€38.34
List Price:  €76.69
You save:  €38.35
£31.81
List Price:  £63.63
You save:  £31.81
CA$57.41
List Price:  CA$114.84
You save:  CA$57.43
A$63.97
List Price:  A$127.96
You save:  A$63.98
S$54.21
List Price:  S$108.43
You save:  S$54.22
HK$310.92
List Price:  HK$621.91
You save:  HK$310.99
CHF 35.74
List Price:  CHF 71.50
You save:  CHF 35.75
NOK kr452.69
List Price:  NOK kr905.49
You save:  NOK kr452.80
DKK kr285.99
List Price:  DKK kr572.05
You save:  DKK kr286.06
NZ$70.70
List Price:  NZ$141.42
You save:  NZ$70.72
د.إ146.88
List Price:  د.إ293.80
You save:  د.إ146.92
৳4,759.62
List Price:  ৳9,520.44
You save:  ৳4,760.81
₹3,397.10
List Price:  ₹6,795.05
You save:  ₹3,397.95
RM180.27
List Price:  RM360.59
You save:  RM180.32
₦61,792.91
List Price:  ₦123,601.28
You save:  ₦61,808.36
₨11,086.54
List Price:  ₨22,175.86
You save:  ₨11,089.31
฿1,367.71
List Price:  ฿2,735.76
You save:  ฿1,368.05
₺1,402.52
List Price:  ₺2,805.39
You save:  ₺1,402.87
B$243.37
List Price:  B$486.81
You save:  B$243.44
R732.54
List Price:  R1,465.27
You save:  R732.72
Лв74.93
List Price:  Лв149.89
You save:  Лв74.95
₩57,842.33
List Price:  ₩115,699.13
You save:  ₩57,856.80
₪145.51
List Price:  ₪291.06
You save:  ₪145.55
₱2,352.81
List Price:  ₱4,706.21
You save:  ₱2,353.40
¥6,256.23
List Price:  ¥12,514.03
You save:  ¥6,257.80
MX$803.05
List Price:  MX$1,606.30
You save:  MX$803.25
QR145.19
List Price:  QR290.43
You save:  QR145.23
P550.51
List Price:  P1,101.16
You save:  P550.64
KSh5,142.14
List Price:  KSh10,285.57
You save:  KSh5,143.43
E£2,034.81
List Price:  E£4,070.14
You save:  E£2,035.32
ብር4,965.06
List Price:  ብር9,931.37
You save:  ብር4,966.30
Kz36,710.82
List Price:  Kz73,430.82
You save:  Kz36,720
CLP$39,506.29
List Price:  CLP$79,022.46
You save:  CLP$39,516.17
CN¥291.78
List Price:  CN¥583.63
You save:  CN¥291.85
RD$2,425.42
List Price:  RD$4,851.45
You save:  RD$2,426.03
DA5,393.73
List Price:  DA10,788.81
You save:  DA5,395.08
FJ$92.63
List Price:  FJ$185.29
You save:  FJ$92.66
Q306.89
List Price:  Q613.86
You save:  Q306.97
GY$8,332.99
List Price:  GY$16,668.06
You save:  GY$8,335.07
ISK kr5,538.46
List Price:  ISK kr11,078.31
You save:  ISK kr5,539.84
DH400.86
List Price:  DH801.83
You save:  DH400.96
L734.57
List Price:  L1,469.33
You save:  L734.75
ден2,358.54
List Price:  ден4,717.67
You save:  ден2,359.13
MOP$318.86
List Price:  MOP$637.80
You save:  MOP$318.94
N$733.28
List Price:  N$1,466.76
You save:  N$733.47
C$1,465.68
List Price:  C$2,931.72
You save:  C$1,466.04
रु5,417.88
List Price:  रु10,837.11
You save:  रु5,419.23
S/148.31
List Price:  S/296.66
You save:  S/148.35
K161.51
List Price:  K323.07
You save:  K161.55
SAR150.21
List Price:  SAR300.46
You save:  SAR150.25
ZK1,102.28
List Price:  ZK2,204.85
You save:  ZK1,102.56
L190.81
List Price:  L381.67
You save:  L190.86
Kč963.45
List Price:  Kč1,927.14
You save:  Kč963.69
Ft15,873.63
List Price:  Ft31,751.23
You save:  Ft15,877.60
SEK kr441.19
List Price:  SEK kr882.49
You save:  SEK kr441.30
ARS$40,705.74
List Price:  ARS$81,421.67
You save:  ARS$40,715.92
Bs275.23
List Price:  Bs550.54
You save:  Bs275.30
COP$174,344.67
List Price:  COP$348,732.94
You save:  COP$174,388.27
₡20,095.61
List Price:  ₡40,196.26
You save:  ₡20,100.64
L1,011.06
List Price:  L2,022.38
You save:  L1,011.31
₲310,574.38
List Price:  ₲621,226.44
You save:  ₲310,652.05
$U1,776.52
List Price:  $U3,553.49
You save:  $U1,776.96
zł163.49
List Price:  zł327.04
You save:  zł163.54
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.