JavaScript Functions Worksheet

Question 1

What is a function, and why would you ever want to use one in your code?

A function is essentially a command in JavaScript. Some functions are prebuilt, while others can be written by the user to shorten the amount of code written, saving time and preventing bugs.
Question 2

What do you call the values that get passed into a function?

Parameters.
Question 3

What is the 'body' of a function, and what are the characters that enclose the body of a function?

The body of a function is the code that the function uses, it's enclosed by curly braces on either end.
Question 4

What does it mean to 'call', or 'invoke' a function (note that 'calling' and 'invoking' a function mean the same thing)?

To call or invoke is to trigger a function after it's been declared, you do this by naming the function and filling in the necessary parameters.
Question 5

If a function has more than one parameter, what character do you use to separate those parameters?

Simply break them up with a comma. Parameters are specified in the parentheses after a function name.
Question 6

What is the problem with this code (explain the syntax error)?


function convertKilometersToMiles(km)
    return km * 0.6217;
}
                

The curly brace is never opened, only closed.
Question 7

In the code below, there are two functions being invoked. Which one returns a value? Explain why you know this.


const name = prompt("Enter your name");
alert("Hello " + name + "!");
                

The 'alert' function returns the value. It shows the user a string concatenation based on its parameters.

Coding Problems

Coding Problems - See the 'script' tag below this h3 tag. You will have to write some JavaScript code in it.

Always test your work! Check the console log to make sure there are no errors.