JavaScript Getting Started With ES2015 Create Functions Using Arrow Syntax Concise Arrow Function Syntax

JavaScript - Arrow Function Expression


So after watching this video, I can understand how a function declaration such as :

function divide1(a, b) { return a / b; }

Can be written as an arrow function expression:

const divide4 = (a, b) => a / b;

My question is, how do we call the arrow function expression.

For example, if i do something like divide4(8,2) i get an error. must i assign the divide(x,y) to a variable in order to use it?

Thanks for reading,

3 Answers

Stuart Wright
Stuart Wright
41,090 Points

I just opened a JavaScript console (in Chrome), and ran the following code:

const divide4 = (a, b) => a / b;
console.log(divide4(8, 2));

It correctly printed out 4. Maybe worth trying again to make sure? Your code seems correct to me.

Thanks, not sure what happened when I tried back then, but all seems to be well now :-)

Vasanth Baskaran
Vasanth Baskaran
4,333 Points

Derek, next time please specify the error too. So that we can narrow it down to what caused the issue to avoid it further.

Thanks, Happy Coding !!!

Kevin Ohlsson
Kevin Ohlsson
4,547 Points


I called the arrow function in workspace, it returned

treehouse:~/workspace$ d(6,3)
bash: syntax error near unexpected token `6,3'

It works on CodePen.

Are workspaces updated to work with es6?