An arrow function is a shorter way to write a function using the => syntax. It makes code simpler and easier to read.
Click to reveal answer
beginner
How do you write a simple arrow function that adds two numbers?
You write it like this: <br><code>const add = (a, b) => a + b;</code><br>This function takes two inputs and returns their sum.
Click to reveal answer
intermediate
What is the difference between a regular function and an arrow function regarding the this keyword?
Arrow functions do not have their own this. They use this from the surrounding code. Regular functions have their own this.
Click to reveal answer
intermediate
Can arrow functions be used as constructors with the new keyword?
No, arrow functions cannot be used as constructors. They do not have a prototype property and will throw an error if used with new.
Click to reveal answer
beginner
How do you write an arrow function with no parameters?
You write it with empty parentheses: <br><code>const greet = () => 'Hello!';</code><br>This function returns the string 'Hello!'.
Click to reveal answer
Which of the following is the correct arrow function syntax to multiply two numbers?
Aconst multiply = (a, b) => { a * b };
Bconst multiply = function(a, b) { return a * b; };
Cconst multiply = (a, b) => a * b;
Dconst multiply = (a, b) => return a * b;
✗ Incorrect
Option C correctly uses arrow function syntax with implicit return. Option B is a regular function. Option A misses the return keyword inside braces. Option D has incorrect syntax.
What happens if you try to use new with an arrow function?
AIt throws an error because arrow functions can't be constructors.
BIt calls the arrow function without creating an object.
CIt returns undefined.
DIt creates a new object as usual.
✗ Incorrect
Arrow functions do not have a prototype and cannot be used as constructors, so using new with them throws an error.
How does the this keyword behave inside an arrow function?
AIt refers to the arrow function itself.
BIt inherits <code>this</code> from the surrounding code.
CIt refers to the global object.
DIt is always undefined.
✗ Incorrect
Arrow functions do not have their own this. They use the this value from the surrounding context.
Which arrow function correctly returns the string 'Hi'?
ABoth B and C
Bconst sayHi = () => 'Hi';
Cconst sayHi = () => { return 'Hi'; };
Dconst sayHi = () => { 'Hi' };
✗ Incorrect
Both B and C correctly return the string 'Hi'. Option D does not return anything because braces without return do not return a value.
Which is a benefit of using arrow functions?
AThey always run faster than regular functions.
BThey automatically bind to all events.
CThey can be used as constructors.
DThey allow shorter syntax and lexical <code>this</code> binding.
✗ Incorrect
Arrow functions provide shorter syntax and use the surrounding this value, which can simplify code. They are not constructors and do not automatically bind to events.
Explain how arrow functions handle the this keyword differently from regular functions.
Think about where <code>this</code> points inside arrow functions.
You got /4 concepts.
Write an arrow function that takes no parameters and returns the string 'Welcome!'.
Remember how to write arrow functions with no inputs.
You got /3 concepts.
Practice
(1/5)
1. What is the main advantage of using arrow functions in JavaScript?
easy
A. They replace all regular functions in JavaScript.
B. They provide a shorter syntax for writing functions.
C. They can only be used for asynchronous code.
D. They automatically create new objects.
Solution
Step 1: Understand arrow function purpose
Arrow functions are designed to write functions in a shorter way compared to regular function expressions.
Step 2: Compare options
Only They provide a shorter syntax for writing functions. correctly states the main advantage: shorter syntax. Other options are incorrect or misleading.
Final Answer:
They provide a shorter syntax for writing functions. -> Option B
Quick Check:
Arrow functions = shorter syntax [OK]
Hint: Arrow functions are mainly about shorter syntax [OK]
Common Mistakes:
Thinking arrow functions create new objects automatically
Believing arrow functions only work with async code
Assuming arrow functions replace all regular functions
2. Which of the following is the correct syntax for an arrow function that adds two numbers a and b?
easy
A. const add = (a, b) => { a + b; };
B. const add = (a, b) { return a + b; };
C. const add = a, b => a + b;
D. const add = (a, b) => a + b;
Solution
Step 1: Check arrow function syntax
Arrow functions can omit braces and return keyword for single expressions, so (a, b) => a + b is valid.
Step 2: Analyze options
A: concise and correct with implicit return. B: missing => operator, syntax error. C: misses parentheses around parameters. D: lacks return statement inside braces.
Final Answer:
const add = (a, b) => a + b; -> Option D
Quick Check:
Concise arrow function = (a, b) => a + b [OK]
Hint: Use parentheses for multiple params and omit braces for single expression [OK]
Common Mistakes:
Omitting parentheses when multiple parameters exist