Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Just like you're able to pass information to a function, you can get information back from a function when it finishes running the code inside the curly braces.
Resources
Practice working with Functions in JavaScript
In this short workshop, you'll practice creating basic JavaScript functions, adding parameters, calling functions and passing values.
Just like you're able to pass information
to a function, you can get information
0:00
back from a function when it finishes
running the code inside the curly braces.
0:04
So now, I'll create a new function called
0:08
add that will add two numbers together and
return the total.
0:13
Functions can accept multiple perimeters,
so I'll have this function
0:18
take two parameters, separated by a comma,
I'll name them a and b.
0:23
Now to return a value from a function
you use the return keyword.
0:28
This creates a return statement
that specifies the value to return.
0:34
So I'll have this function
return the sum of a plus b.
0:38
Creating a function that returns a value
makes the function flexible enough that
0:45
you can use it in many
different situations.
0:49
So for example, this time I'll output
the value the add function returns to
0:52
the browser console using console.log.
0:56
And console.log works similar to alert and
prompt.
1:01
You put something inside
the parentheses and
1:04
it's displayed and/or printed
to the browser console.
1:06
So for example, I'll pass console.log,
a call to the add function,
1:09
and then I'll pass add to arguments 20 and
30, separated by a comma.
1:15
And right below, let's call add
inside a second console.log.
1:21
This time I'll pass add
the arguments 120 and 310.
1:27
You'll use console.log
frequently while programming.
1:34
As you'll see later, console.log comes in
handy when trying to figure out what's
1:39
going on inside a program.
1:42
You can use it to print messages as your
program's run and help debug your code.
1:44
So in this case, the return value
is given to each add function call.
1:49
So over in the browser,
you'll see the numbers 50 and
1:55
430 get logged to the console.
1:59
And you could also, for example, make this
a function that multiplies two values and
2:02
returns the result.
2:07
So I will change add to multiply, and
2:08
I will return a * b using
the asterisks symbol,
2:12
and the console let's call multiply once,
2:17
inside a console.log and
I will pass it the arguments,6 and
2:22
8, give that a save, refresh,
and this logs the number 48.
2:28
JavaScript functions are a valuable
tool in your programming toolkit.
2:35
Functions can let you run a dozen or
2:39
hundreds of lines of programming by
typing just a single line of code.
2:41
You'll use them constantly
in your JavaScript programs
2:46
to make your programs more modular and
easier to update.
2:49
You need to sign up for Treehouse in order to download course files.
Sign up