Code highlights logo

Learn

Functions

Arrow Functions

Arrow functions offer several benefits over traditional function expressions. They provide a more compact syntax with less code, making your codebase more readable and maintainable.

Syntax

The syntax for an arrow function looks like this:

1const functionName = (parameter1, parameter2) => {
2 // Function body
3 // Code goes here
4};

The arrow (=>) separates the function parameters from the function body, which is enclosed in curly braces ({}). If the function has only one parameter, you can omit the parentheses around the parameter list. If the function has no parameters, you must include empty parentheses.

Examples

Here are a few examples to illustrate the usage of arrow functions:

1// Basic arrow function
2const square = (num) => {
3 return num * num;
4};

Instructions

1.

Change the sum function into an arrow function.

2.

Call and log the new sum function with two arguments of your choice.

Sign up to start coding

Already have an account?

Sign In

Course content