Intro to JavaScript Functions Arrow Functions
Learning objective: By the end of this lesson, students will be capable of converting traditional function expressions into arrow function expressions and explain the syntactical differences between the two.
Syntax
Arrow function expression syntax is the most common and modern syntax. You’ll see us use it more often than not.
The following function expression:
const add = function(numA, numB) {
return numA + numB;
}
is equivalent to the following function written as an arrow function expression:
const add = (numA, numB) => {
return numA + numB;
}
Arrow function expressions offer a more concise syntax compared to function expressions while using many of the same patterns. We can leave off the function keyword entirely when composing them. Here’s a breakdown:

- The
constkeyword.constshould be used whenever a function expression is assigned to a variable. - The name of the function.
- Comma-separated parameters. Note that if there are no parameters, the
()is required. - The arrow:
=>. Note that this replaces thefunctionkeyword. This is the only required syntax change between function expressions and arrow function expressions. - The body of the function is indicated by curly braces.
- 5a. The statements that make up the function itself.
- 5b. Optionally, a
returnstatement.
🎓 You Do
Let’s practice using this new syntax by converting a function declaration into an arrow function expression.
-
Take a look at the following function that squares a number:
function square(num) { return num * num; } -
Convert this function into an arrow function expression.
❓ Review questions
Given this code:
const sumTwoNumbers = (numA, numB) => {
return numA + numB;
}
const sum = sumTwoNumbers(5, 10);
- What is the job of the
sumTwoNumbersfunction? - Is
numAa parameter or an argument? - What will the value of
numAbe inside thesumTwoNumbersfunction? - Will
numAhave a value outside of thesumTwoNumbersfunction? If it does, what will it be? - What data type will the value returned from the
sumTwoNumbersfunction be? - What will be returned from the
sumTwoNumbersfunction? - What would print to the console if we logged
sumafter the final line? - How many arguments are passed to the
sumTwoNumbersfunction? What are they? - How many parameters does the
sumTwoNumbersfunction accept? What are they?
Given this code:
const emphasize = (str) => {
return `${str} ${str}!`;
}
emphasize('really');
- What does the
emphasizefunction do? - What will the value of
strbe inside of theemphasizefunction? - What data type will the value returned from the
emphasizefunction be? - Will what is returned from the
emphasizefunction be usable later in this program? If not, how could we make it so that it is? - How could we make it so that what is returned from the
emphasizefunction is shown in the console? - How many arguments are passed to the
emphasizefunction? What are they? - How many parameters does the
emphasizefunction accept? What are they?