Learn
Functions
Concise Body Arrow Functions
Concise body arrow functions provide a more compact and elegant way to write functions, without compromising functionality. This syntax is especially useful in scenarios where we need to write short, one-liner functions or when we want to streamline our code.
To define a concise body arrow function, we use the following syntax:
The parentheses can be omitted if there is only one parameter, and the function has no parameters at all. Similarly, if the function body is a single expression, we can omit the curly braces.
Let's go over some examples to illustrate how concise body arrow functions work:
Example 1:
Here, we define a function named multiply
that takes two parameters, a
and b
, and returns their product. Since the function body is a single expression, we don't need curly braces.
Example 2:
In this example, we define a function named isEven
that takes a single parameter, number
. The function body consists of a boolean expression that checks if the number is even. Again, since the function body is a single expression, we don't need curly braces.
Instructions
Convert the multiply
function to a concise body arrow function.
Convert the divide
function to a concise body arrow function.
Sign up to start coding
Already have an account?
Sign In