The Rest Parameter

4 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.34
List Price:  €68.70
You save:  €34.35
£29.75
List Price:  £59.51
You save:  £29.76
CA$55.26
List Price:  CA$110.54
You save:  CA$55.28
A$61.34
List Price:  A$122.71
You save:  A$61.36
S$51.56
List Price:  S$103.13
You save:  S$51.57
HK$311.87
List Price:  HK$623.83
You save:  HK$311.95
CHF 32.19
List Price:  CHF 64.39
You save:  CHF 32.20
NOK kr403.80
List Price:  NOK kr807.72
You save:  NOK kr403.91
DKK kr256.36
List Price:  DKK kr512.80
You save:  DKK kr256.43
NZ$68.25
List Price:  NZ$136.51
You save:  NZ$68.26
د.إ146.86
List Price:  د.إ293.76
You save:  د.إ146.90
৳4,863.12
List Price:  ৳9,727.46
You save:  ৳4,864.33
₹3,524.96
List Price:  ₹7,050.81
You save:  ₹3,525.84
RM169.05
List Price:  RM338.15
You save:  RM169.10
₦61,064.73
List Price:  ₦122,144.73
You save:  ₦61,080
₨11,267.18
List Price:  ₨22,537.18
You save:  ₨11,270
฿1,293.08
List Price:  ฿2,586.50
You save:  ฿1,293.41
₺1,646.44
List Price:  ₺3,293.30
You save:  ₺1,646.85
B$218.38
List Price:  B$436.81
You save:  B$218.43
R711.68
List Price:  R1,423.54
You save:  R711.85
Лв67.10
List Price:  Лв134.21
You save:  Лв67.11
₩55,741.82
List Price:  ₩111,497.59
You save:  ₩55,755.76
₪134.54
List Price:  ₪269.12
You save:  ₪134.57
₱2,284.41
List Price:  ₱4,569.39
You save:  ₱2,284.98
¥5,931.38
List Price:  ¥11,864.24
You save:  ¥5,932.86
MX$750.38
List Price:  MX$1,500.95
You save:  MX$750.57
QR145.58
List Price:  QR291.21
You save:  QR145.62
P536.90
List Price:  P1,073.94
You save:  P537.04
KSh5,166.70
List Price:  KSh10,334.70
You save:  KSh5,168
E£1,941.39
List Price:  E£3,883.26
You save:  E£1,941.87
ብር5,716.16
List Price:  ብር11,433.76
You save:  ብር5,717.59
Kz36,469.08
List Price:  Kz72,947.28
You save:  Kz36,478.20
CLP$38,726.71
List Price:  CLP$77,463.11
You save:  CLP$38,736.40
CN¥285.16
List Price:  CN¥570.39
You save:  CN¥285.23
RD$2,518.39
List Price:  RD$5,037.41
You save:  RD$2,519.02
DA5,195.45
List Price:  DA10,392.21
You save:  DA5,196.75
FJ$90.43
List Price:  FJ$180.89
You save:  FJ$90.46
Q306.32
List Price:  Q612.73
You save:  Q306.40
GY$8,355.08
List Price:  GY$16,712.25
You save:  GY$8,357.17
ISK kr4,932.76
List Price:  ISK kr9,866.76
You save:  ISK kr4,934
DH363.26
List Price:  DH726.63
You save:  DH363.36
L668.13
List Price:  L1,336.43
You save:  L668.29
ден2,110.62
List Price:  ден4,221.77
You save:  ден2,111.14
MOP$320.92
List Price:  MOP$641.92
You save:  MOP$321
N$704.48
List Price:  N$1,409.15
You save:  N$704.66
C$1,469.67
List Price:  C$2,939.71
You save:  C$1,470.03
रु5,632.07
List Price:  रु11,265.56
You save:  रु5,633.48
S/141.26
List Price:  S/282.56
You save:  S/141.30
K166.62
List Price:  K333.28
You save:  K166.66
SAR150.04
List Price:  SAR300.13
You save:  SAR150.08
ZK949.74
List Price:  ZK1,899.73
You save:  ZK949.98
L174.39
List Price:  L348.82
You save:  L174.43
Kč839.73
List Price:  Kč1,679.67
You save:  Kč839.94
Ft13,526.45
List Price:  Ft27,056.29
You save:  Ft13,529.83
SEK kr378.31
List Price:  SEK kr756.72
You save:  SEK kr378.41
ARS$54,428.90
List Price:  ARS$108,871.42
You save:  ARS$54,442.52
Bs275.96
List Price:  Bs551.99
You save:  Bs276.03
COP$160,141.69
List Price:  COP$320,323.44
You save:  COP$160,181.74
₡20,189.65
List Price:  ₡40,384.34
You save:  ₡20,194.69
L1,046.93
List Price:  L2,094.13
You save:  L1,047.20
₲288,443.91
List Price:  ₲576,959.95
You save:  ₲288,516.04
$U1,602.41
List Price:  $U3,205.22
You save:  $U1,602.81
zł146.11
List Price:  zł292.25
You save:  zł146.14
Already have an account? Log In

Transcript

Sometimes you are not sure of how many parameters you need to pass to a specific function. And that could be for many, many reasons. And fortunately, JavaScript has also solved this problem for people who are not sure of the number of parameters that they need to pass to a specific function that they create. So suppose that you are going to create a function that is going to take the names of all employees of a specific company, it would be impossible to pass all of these names, especially if the company has thousands of employees. Therefore, we have something called a various parameter, which is going to take an infinite number of parameters. It doesn't matter how many parameters you intend to pass the two specific function you can pass as many fun as you can.

Pass as many parameters as you want. So for example, here, I'm going to create a function function. And it's going to be employees, employees are names. and here if I wanted to pass the names of all employees, I would type for example, name one, name one, and then name two, and then Name three, and, and then name four, until, for example, name 1000, that would be absolutely tedious and impractical and a waste of time. Therefore, in order to solve this problem, you can use the rest parameter, which has a syntax of three dots, and then name and this syntax. What that means is that now you can pass as many parameters to this function as you want, and I'm going to prove it to you here.

I'm going to Use the console. Con. So that log, and here I'm going to just print the name, this parameter. And let's call this function and see what this parameter is all about is all about. So here I'm going to save and run. And as you can see, what I get is an array.

And what that means is that this parameter is an array, which means that each time you pass a parameter, it is going to be stored into this array. And you can access this array as a normal access that variable as a normal, very normal item in an array. And of course, if I use the square bracket notation and add zero, and if I save and run on run, I'm going to get undefined because this array now is empty is an empty array. But suppose that I pass here, john, as the first name, john If I save on run, I'm going to get john because I used the first index, which is zero. And john now has been added to this array to the name array as the first value or the first item. And if I just print the name array, if I save and run, I'm going to get john inside this array.

If I want to pass another parameter, I just add comma, and here, just add K. And if I save on run, I'm going to get k, john and Kate and Kate will be at the second position in this array, which means it will have an index of one. So now you can pass as many parameters to this function as you want. It doesn't even matter. All you need to do here is once you start once you call the function, you just pass the parameter that you want to pass to this function. And inside this function you can access exactly which parameter that you want to use. So in conclusion, the rest parameter is very powerful in case that you don't know how many parameters you want to pass or you are unsure till the moment of how many parameters you want to pass to your function.

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.