Defining Functions

Modern JavaScript Incorporating Functions
3 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€85.51
List Price:  €119.72
You save:  €34.20
£73.90
List Price:  £103.47
You save:  £29.56
CA$136.90
List Price:  CA$191.66
You save:  CA$54.76
A$151.80
List Price:  A$212.53
You save:  A$60.72
S$127.94
List Price:  S$179.12
You save:  S$51.18
HK$784.91
List Price:  HK$1,098.91
You save:  HK$313.99
CHF 79.57
List Price:  CHF 111.40
You save:  CHF 31.83
NOK kr1,010.31
List Price:  NOK kr1,414.48
You save:  NOK kr404.16
DKK kr638.05
List Price:  DKK kr893.30
You save:  DKK kr255.24
NZ$166.08
List Price:  NZ$232.52
You save:  NZ$66.44
د.إ367.26
List Price:  د.إ514.18
You save:  د.إ146.92
৳12,166.73
List Price:  ৳17,033.91
You save:  ৳4,867.18
₹8,576.47
List Price:  ₹12,007.40
You save:  ₹3,430.93
RM425.20
List Price:  RM595.30
You save:  RM170.10
₦153,083.69
List Price:  ₦214,323.29
You save:  ₦61,239.60
₨28,457.96
List Price:  ₨39,842.28
You save:  ₨11,384.32
฿3,241.77
List Price:  ฿4,538.61
You save:  ฿1,296.84
₺4,016.97
List Price:  ₺5,623.92
You save:  ₺1,606.95
B$553.52
List Price:  B$774.95
You save:  B$221.43
R1,783.60
List Price:  R2,497.11
You save:  R713.51
Лв167.26
List Price:  Лв234.17
You save:  Лв66.91
₩137,424.86
List Price:  ₩192,400.31
You save:  ₩54,975.44
₪333.46
List Price:  ₪466.86
You save:  ₪133.39
₱5,649.68
List Price:  ₱7,909.78
You save:  ₱2,260.09
¥14,680.13
List Price:  ¥20,552.77
You save:  ¥5,872.64
MX$1,867.95
List Price:  MX$2,615.20
You save:  MX$747.25
QR364.18
List Price:  QR509.86
You save:  QR145.68
P1,333.20
List Price:  P1,866.54
You save:  P533.33
KSh12,918.70
List Price:  KSh18,086.70
You save:  KSh5,168
E£4,950.01
List Price:  E£6,930.21
You save:  E£1,980.20
ብር13,536.14
List Price:  ብር18,951.14
You save:  ብር5,415
Kz91,186.39
List Price:  Kz127,664.59
You save:  Kz36,478.20
CLP$94,996.49
List Price:  CLP$132,998.89
You save:  CLP$38,002.40
CN¥716.72
List Price:  CN¥1,003.44
You save:  CN¥286.72
RD$6,015.80
List Price:  RD$8,422.37
You save:  RD$2,406.56
DA12,975.83
List Price:  DA18,166.68
You save:  DA5,190.85
FJ$224.10
List Price:  FJ$313.75
You save:  FJ$89.65
Q768.63
List Price:  Q1,076.11
You save:  Q307.48
GY$20,933.06
List Price:  GY$29,307.13
You save:  GY$8,374.06
ISK kr12,176.78
List Price:  ISK kr17,047.98
You save:  ISK kr4,871.20
DH899.63
List Price:  DH1,259.52
You save:  DH359.89
L1,695.54
List Price:  L2,373.83
You save:  L678.28
ден5,263.48
List Price:  ден7,369.09
You save:  ден2,105.60
MOP$809
List Price:  MOP$1,132.64
You save:  MOP$323.63
N$1,776.31
List Price:  N$2,486.90
You save:  N$710.59
C$3,679
List Price:  C$5,150.76
You save:  C$1,471.75
रु13,709.03
List Price:  रु19,193.20
You save:  रु5,484.16
S/354.56
List Price:  S/496.40
You save:  S/141.84
K412.43
List Price:  K577.42
You save:  K164.99
SAR375.01
List Price:  SAR525.03
You save:  SAR150.02
ZK2,423.83
List Price:  ZK3,393.47
You save:  ZK969.63
L434.35
List Price:  L608.12
You save:  L173.76
Kč2,108.68
List Price:  Kč2,952.24
You save:  Kč843.56
Ft34,161.31
List Price:  Ft47,827.21
You save:  Ft13,665.89
SEK kr953.06
List Price:  SEK kr1,334.33
You save:  SEK kr381.26
ARS$125,561.90
List Price:  ARS$175,791.68
You save:  ARS$50,229.78
Bs688.51
List Price:  Bs963.94
You save:  Bs275.43
COP$401,926.41
List Price:  COP$562,713.05
You save:  COP$160,786.64
₡50,460.13
List Price:  ₡70,646.21
You save:  ₡20,186.07
L2,631.26
List Price:  L3,683.87
You save:  L1,052.60
₲775,455.63
List Price:  ₲1,085,668.91
You save:  ₲310,213.27
$U4,043.06
List Price:  $U5,660.45
You save:  $U1,617.38
zł363.84
List Price:  zł509.39
You save:  zł145.55
Already have an account? Log In

Transcript

Let's take a look at how we go about defining functions for use in our own JavaScript code. So first, let's take a look at the syntax. in defining a function, you must first use the function keyword. As the first part of that, then you put the function name. Now in certain situations that function name is optional, and in those cases, it's called an anonymous function. Following that function name, you have a set of parentheses, that is not optional.

Then you have the curly braces, the curly braces enclose the, the code block that will execute anytime the function is called. And those curly braces are not optional. Either they must be there. That code block may be a single statement, or it can be multiple statements. Now once you've defined a function, you can then Call it or invoke it. by calling or invoking the function, it causes the code within the function to execute.

The syntax for calling a function is simply the function name. And then parentheses is actually the parentheses that tell the JavaScript engine to execute that code. Without the parentheses, it would just display the code you want to execute, so the parentheses are required. Now when that function is defined as part of an object, then you use the dot syntax to call that function. And in that case, we did we refer to it as a method. And we've done that multiple times in past movies already.

So the object dot method name and then parentheses to call the method of the object. All right, let me jump out to the console and we'll do a quick example. I'm going to define a very simple function called greeting. Here I have the function keyword, I have the function name, which is greeting the parentheses, then the curly braces inside the curly braces is the code block. This is the code that will execute when the function is called. As you can see, right now we have a single line, all it does is print hello to the console.

To call that I would need to enter greeting, and then parentheses. Notice what happens if I just enter greeting is simply displays the code that defines that function. If I want that to execute, I need to include the parentheses in there it executes and prints hello to the console. So that's how simple it is to define a function and then invoke that function. Most programs consist of many functions. And how do you determine if you need to use a function?

Well in the past movie, we went through some of the advantages of functions. If your code can fit into one of those advantages, then define a function. As you get used to writing more and more JavaScript programs. It will become more intuitive to you when to do a function. Basically, you think, Oh, I need to solve this little issue. That's a part of my requirements that will be a function.

Alright, now that we know how to define a function, let's look at the difference between a function declaration and a function expression.

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.