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.84
List Price:  €120.19
You save:  €34.34
£74.36
List Price:  £104.11
You save:  £29.74
CA$137.67
List Price:  CA$192.74
You save:  CA$55.07
A$153.33
List Price:  A$214.67
You save:  A$61.33
S$128.46
List Price:  S$179.85
You save:  S$51.38
HK$784.91
List Price:  HK$1,098.91
You save:  HK$313.99
CHF 80.95
List Price:  CHF 113.33
You save:  CHF 32.38
NOK kr1,023.62
List Price:  NOK kr1,433.11
You save:  NOK kr409.49
DKK kr640.71
List Price:  DKK kr897.02
You save:  DKK kr256.31
NZ$168.27
List Price:  NZ$235.59
You save:  NZ$67.31
د.إ367.26
List Price:  د.إ514.18
You save:  د.إ146.92
৳12,133.03
List Price:  ৳16,986.72
You save:  ৳4,853.69
₹8,768.17
List Price:  ₹12,275.79
You save:  ₹3,507.62
RM423.30
List Price:  RM592.64
You save:  RM169.34
₦153,196.67
List Price:  ₦214,481.47
You save:  ₦61,284.80
₨28,345.01
List Price:  ₨39,684.15
You save:  ₨11,339.14
฿3,239.04
List Price:  ฿4,534.79
You save:  ฿1,295.74
₺4,070.32
List Price:  ₺5,698.61
You save:  ₺1,628.29
B$543.33
List Price:  B$760.69
You save:  B$217.35
R1,773
List Price:  R2,482.27
You save:  R709.27
Лв167.97
List Price:  Лв235.16
You save:  Лв67.19
₩139,039.12
List Price:  ₩194,660.33
You save:  ₩55,621.21
₪341.84
List Price:  ₪478.59
You save:  ₪136.75
₱5,702.27
List Price:  ₱7,983.41
You save:  ₱2,281.13
¥14,772.31
List Price:  ¥20,681.83
You save:  ¥5,909.51
MX$1,858.13
List Price:  MX$2,601.46
You save:  MX$743.32
QR365
List Price:  QR511.01
You save:  QR146.01
P1,344.08
List Price:  P1,881.76
You save:  P537.68
KSh12,913.70
List Price:  KSh18,079.70
You save:  KSh5,166
E£4,848.51
List Price:  E£6,788.11
You save:  E£1,939.60
ብር13,861.38
List Price:  ብር19,406.49
You save:  ብር5,545.10
Kz91,186.39
List Price:  Kz127,664.59
You save:  Kz36,478.20
CLP$96,811.31
List Price:  CLP$135,539.71
You save:  CLP$38,728.40
CN¥718.28
List Price:  CN¥1,005.63
You save:  CN¥287.34
RD$6,100.53
List Price:  RD$8,540.98
You save:  RD$2,440.45
DA12,980.75
List Price:  DA18,173.57
You save:  DA5,192.82
FJ$225.41
List Price:  FJ$315.58
You save:  FJ$90.17
Q766.24
List Price:  Q1,072.76
You save:  Q306.52
GY$20,893.14
List Price:  GY$29,251.24
You save:  GY$8,358.09
ISK kr12,276.77
List Price:  ISK kr17,187.97
You save:  ISK kr4,911.20
DH904.36
List Price:  DH1,266.14
You save:  DH361.78
L1,676.84
List Price:  L2,347.65
You save:  L670.80
ден5,271.38
List Price:  ден7,380.15
You save:  ден2,108.76
MOP$807.42
List Price:  MOP$1,130.42
You save:  MOP$323
N$1,770.11
List Price:  N$2,478.23
You save:  N$708.11
C$3,676.14
List Price:  C$5,146.75
You save:  C$1,470.60
रु13,995.24
List Price:  रु19,593.90
You save:  रु5,598.65
S/353.46
List Price:  S/494.87
You save:  S/141.40
K421.35
List Price:  K589.91
You save:  K168.55
SAR375.25
List Price:  SAR525.37
You save:  SAR150.11
ZK2,315.06
List Price:  ZK3,241.18
You save:  ZK926.11
L434.92
List Price:  L608.91
You save:  L173.98
Kč2,102.56
List Price:  Kč2,943.68
You save:  Kč841.11
Ft33,957.78
List Price:  Ft47,542.25
You save:  Ft13,584.47
SEK kr959.49
List Price:  SEK kr1,343.32
You save:  SEK kr383.83
ARS$132,539.68
List Price:  ARS$185,560.86
You save:  ARS$53,021.17
Bs690.03
List Price:  Bs966.06
You save:  Bs276.03
COP$404,584.84
List Price:  COP$566,434.96
You save:  COP$161,850.12
₡50,617.43
List Price:  ₡70,866.43
You save:  ₡20,248.99
L2,615.69
List Price:  L3,662.07
You save:  L1,046.38
₲748,183.91
List Price:  ₲1,047,487.40
You save:  ₲299,303.49
$U4,005.02
List Price:  $U5,607.19
You save:  $U1,602.16
zł365.06
List Price:  zł511.10
You save:  zł146.04
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.