JavaScript Functions Worksheet
Question 1What is a function, and why would you ever want to use one in your code?
A function is a block of code that does a specific task. You use functions to organize your code and avoid repeating yourself.
What do you call the values that get passed into a function?
The values passed into a function are called arguments.
Do all functions return a value? (Note: this is a bit of a trick question - make sure to ask me about this in class.)
No, some functions don't return a value. They perform actions and return undefined by default.
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 inside it, and it's enclosed by curly braces
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 a function means to run its code.
If a function has more than one parameter, what character do you use to separate those parameters?
You use a comma to separate multiple parameters in a function.
What is the problem with this code (explain the syntax error)?
function convertKilometersToMiles(km) return km * 0.6217; }
The problem with the code is it's missing the opening
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 prompt function returns the user's input. The alert function does not return a value; it just displays a message.
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.