Recall & Review
beginner
What is a function declaration in JavaScript?
A function declaration defines a named function using the
function keyword, followed by the function name, parentheses for parameters, and a block of code inside curly braces. It can be called anywhere in the code after or before its declaration.Click to reveal answer
beginner
How do you declare a function named
greet that prints 'Hello!'?You write: <pre>function greet() {<br> console.log('Hello!');<br>}</pre>Click to reveal answer
intermediate
Can you call a function declared with a function declaration before it appears in the code?
Yes, function declarations are hoisted, so you can call them before their actual declaration in the code.
Click to reveal answer
intermediate
What is the difference between a function declaration and a function expression?
A function declaration defines a named function and is hoisted, so it can be called before it appears. A function expression assigns a function to a variable and is not hoisted in the same way, so it can only be called after the assignment.
Click to reveal answer
beginner
Write a function declaration named
add that takes two numbers and returns their sum.function add(a, b) {<br> return a + b;<br>}Click to reveal answer
Which keyword is used to declare a function in JavaScript?
✗ Incorrect
The
function keyword is used to declare functions in JavaScript.What will happen if you call a function declared with a function declaration before its code appears?
✗ Incorrect
Function declarations are hoisted, so you can call them before their declaration.
Which of the following is a correct function declaration?
✗ Incorrect
Option A is a function declaration. Options B and C are function expressions or arrow functions. Option D is invalid in JavaScript.
What is the output of this code?<br>
function test() { return 5; }<br>console.log(test());✗ Incorrect
The function returns 5, so console.log prints 5.
Which statement about function declarations is TRUE?
✗ Incorrect
Function declarations are hoisted and can be called before their declaration.
Explain what a function declaration is and how it differs from a function expression.
Think about how you write the function and when you can call it.
You got /5 concepts.
Write a function declaration named
multiply that takes two numbers and returns their product.Use the function keyword and return the multiplication of the two inputs.
You got /5 concepts.