Functions Flashcards
What is the concise syntax for an arrow function with one parameter?
You can omit the parentheses.
How would you define the square function using a concise arrow function syntax?
const square = x => x * x.
What happens when you try to declare an arrow function with multiple parameters without parentheses?
It throws a syntax error.
Provide the correct syntax for declaring an arrow function with multiple parameters.
const getArea = (width, length, unit) => { /* code */ }.
What is an implicit return in arrow functions?
The value is returned automatically without the return keyword.
How can you implicitly return a single line of code in an arrow function?
Omit the return keyword and curly braces.
Write an example of an arrow function that adds two numbers using implicit return.
const add = (a, b) => a + b.
What is the correct way to write an arrow function with no parameters?
const greeting = () => alert(Greetings, ${name}!
);
True or False: Arrow functions with no parameters require parentheses before the arrow token.
True.
What is the result of using the following syntax: const greeting = => alert(Greetings, ${name}!
);
Uncaught SyntaxError: Unexpected token ‘=>’
In what scenario can you use parentheses to implicitly return a multiline block of code?
When you wrap the lines in parentheses instead of curly braces.
List the functions that can be written using implicit return.
- const square = x => x * x;
- const multiply = (x, y) => x * y;
- const add = (a, b) => a + b;
- const subtract = (a, b) => a - b;
What does the following code return: const getArea = (width, length, unit) => { const area = width * length; return ${area} ${unit}
; }?
It returns the area in the specified unit.
Fill in the blank: Arrow functions are less verbose than _______.
function declarations.