JavaScript For Kids For Dummies
Book image
Explore Book Buy On Amazon

Functions in JavaScript have a special vocabulary and way that they must be written and used. To really understand functions, you need to be able to speak their language. So, look at a couple words and take apart a function to see what's inside!

Defining a function

When you write a function, that's called defining it. Defining a function makes the code inside that function available to be run.

There are a couple different ways to define a function. The most common way is to use the function keyword, followed by the name of the function, followed by parentheses and then curly braces, like this:

function myFunction() {
  // statements go here
}

Another way to define a function is by using the new Function technique. That looks like this:

var myFunction = new Function() {
  // statements go here
}

Both methods get the job done, but the first technique is the more common technique.

Giving the function a head

The first part of a function definition is called the function head. The function head includes the function keyword, the name of the function, and the parentheses:

function myFunction()

Filling out the function body

Next up is the function body. The function body is made up of statements, surrounded by curly braces. For example:

{
  // this is the function body
}

Calling a function

When you run the code within a function body, that's called calling the function. To call a function, you simply write the name of the function, followed by parentheses. For example:

myFunction();

Defining parameters

Parameters are values that can be included between the parentheses when a function is called. To define a parameter, simply give the parameter a name and put it between the parentheses in the function definition. For example:

function myFunction(theText) {
}

You can define multiple parameters by separating them with commas.

Passing arguments

When you call a function using a value between the parentheses, it's called passing an argument. For example:

myFunction("This is some text");

In this case, the argument is the string "This is some text".

When you're defining a function, the values between the parentheses are called parameters. When passing values into a function, they're called arguments.

When you pass an argument into a function, the function automatically creates a new variable with the name of the parameter and gives it the value of the argument that you passed.

Returning a value

When you call a function and (optionally) pass it an argument, the function starts doing its thing. After the function completes its task, it stops running and produces some sort of value. The value that a function produces when it finishes running is called its return value.

You can set what the return value is by using the return statement. For example, the following function will always return the number 3000:

function whatsTheNumber(){
  return 3000;
}

To find out or use the return value of a function, you can call the function as part of an operation. For example, to do math with the result of a function, you just include the function as a normal operand, like this:

var theTotal = whatsTheNumber() + 80;

When you run this statement, a value equal to the return value of whatsTheNumber() plus 80 (or 3080) will be assigned to theTotal.

If you don't specify a return value for a function, the function will return undefined.

About This Article

This article is from the book:

About the book authors:

Chris Minnick and Eva Holland are experienced web developers, tech trainers, and coauthors of Coding with JavaScript For Dummies. Together they founded WatzThis?, a company focused on training and course development.

This article can be found in the category: