Arrow Functions Learn JavaScript Ready
Arrow Functions Learn JavaScript Ready
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
1
console.log(add(2, 3)); // Output: 5
console.log(addArrow(2, 3)); // Output: 5
console.log(square(4)); // Output: 16
console.log(squareArrow(4)); // Output: 16
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
2
sayHelloArrow(); // Output: Hello, World!
4. Implicit Return:
// Traditional function expression
const multiply = function (a, b) {
return a * b;
};
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
3
console.log(squared); // Output: [1, 4, 9, 16, 25]
console.log(squaredArrow); // Output: [1, 4, 9, 16, 25]
Arrow functions have a concise syntax and lexically bind the this value, making
them particularly useful in certain situations. However, it's important to note that
they are not a direct replacement for traditional functions in all cases, especially
when dealing with object methods or constructors.
Steps:
1. Define an arrow function with the name greet and a parameter name.
2. Use template literals to create a greeting message.
3. Return the greeting message.
Code:
const greet = name => `Hello, ${name}!`;
console.log(greet('John')); // Output: Hello, John!
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
4
Steps:
1. Define an arrow function with the name squareNumbers and a parameter
numbers.
2. Use the map function to square each number in the array.
3. Return the new array.
Code:
const squareNumbers = numbers => numbers.map(num => num
* num);
console.log(squareNumbers([1, 2, 3, 4])); // Output:
[1, 4, 9, 16]
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
5
console.log(filterEvenNumbers([1, 2, 3, 4, 5])); //
Output: [2, 4]
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
6
3. Return the result.
Code:
const powerFunction = (base, exponent) =>
Math.pow(base, exponent);
console.log(powerFunction(2, 3)); // Output: 8
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
7
Steps:
1. Define an arrow function with the name celsiusToFahrenheit and a
parameter celsius.
2. Use the conversion formula: F = (C * 9/5) + 32.
3. Return the temperature in Fahrenheit.
Code:
const celsiusToFahrenheit = celsius => (celsius * 9/5)
+ 32;
console.log(celsiusToFahrenheit(25)); // Output: 77
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
8
console.log(greetWithDefault('Bob', 'Hi')); // Output:
Hi, Bob!
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
9
Code:
const calculator = {
add: (a, b) => a + b,
subtract: (a, b) => a - b,
multiply: (a, b) => a * b,
divide: (a, b) => (b !== 0) ? a / b : 'Cannot divide
by zero',
};
Quiz Questions:
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
10
B) To replace traditional functions
C) To add new features to the language
D) To enforce strict typing
Answer: A) To improve code readability
A) ::
B) =>
C) ->
D) :::
Answer: B) =>
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
11
Question: What does the map function do when used with an arrow
function?
A) reduce
B) filter
C) map
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
12
D) forEach
Answer: B) filter
Question: What is the purpose of the reduce function when used with
an arrow function?
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
13
Question: What is the purpose of the rest parameter (...args) in an
arrow function?
Question: How can you create an arrow function to calculate the power
of a number (base to the exponent)?
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
14