JS Arrow Functions

10 min read

What is JS Arrow Functions?

A JavaScript function is a block of code designed to perform a particular task. Functions are executed when they are invoked (called). You can define functions using the function keyword or as arrow functions with =>.

Syntax & Usage

The syntax and usage patterns for JS Arrow Functions follow standard JavaScript conventions. Below you will see code examples that demonstrate the most common use cases. Try modifying these examples in your own editor to experiment with different approaches.

Example

Here is a practical example demonstrating JS Arrow Functions:

Tips & Best Practices

  • Use const by default, let when you need to reassign
  • Use strict equality (===) instead of loose equality (==)
  • Handle errors with try/catch blocks for robust code
  • Use template literals instead of string concatenation

Summary

In this lesson you learned about JS Arrow Functions. Practice writing code to reinforce these concepts and move on to the next lesson when you feel comfortable.

Examples

Example
javascript
// Traditional function
function add(a, b) {
  return a + b;
}

// Arrow function
const multiply = (a, b) => a * b;

// Arrow function with body
const greet = (name) => {
  const message = `Hello, ${name}!`;
  return message;
};

console.log(add(2, 3));      // 5
console.log(multiply(4, 5)); // 20
console.log(greet('World')); // Hello, World!
Output
5 20 Hello, World!

Test Your Knowledge

Take a quick quiz to check what you've learned in this lesson.

JS Arrow Functions Quiz

1

Which keyword declares a variable that cannot be reassigned?

Explanation: const declares a variable whose value cannot be reassigned after initialization.
2

What does === check in JavaScript?

Explanation: The strict equality operator (===) checks both value and type without type coercion.
3

Which method adds an element to the end of an array?

Explanation: The push() method adds one or more elements to the end of an array.

Related articles