Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Functions Arrow Functions Create an Arrow Function

Kevin Tucker
Kevin Tucker
4,987 Points

Arrow Functions

I've gone through videos but I still can't solve this challenge, could someone shed some light? Its driving me nuts.

script.js
greet('cool coders');

function greet(val) {
  return `Hi, ${val}!`;
}

4 Answers

Dimitar Dimitrov
Dimitar Dimitrov
11,800 Points

First you need to declare the function expression and then call it because arrow functions are not hoisted on the top of the code

let greet = (val) => {
  return `Hi, ${val}!`;
}

greet('cool coders');
Kevin Tucker
Kevin Tucker
4,987 Points

Thank you for that. What does this section of code do: greet('cool coders');. I kept trying to include in the arrow function which is where I was going wrong.

Dimitar Dimitrov
Dimitar Dimitrov
11,800 Points

The greet("cool coders") code is where the function is called with string argument "cool coders"

Dimitar's solution is a perfectly fine one. You could even simplify it further with the following:

const greet = x => `Hi, x`;
greet('cool coders');

Thus utilizing the implicit return w/o curly braces concise syntax. https://teamtreehouse.com/library/javascript-functions/arrow-functions/concise-arrow-function-syntax

Arrow expressions are not my strong suit for sure. One of my mistakes ('for the past hour') was writing a whole separate code along with the code already given to me. Of course kept getting the 'Greet function has already been defined' error. This community helped me straighten that mistake out. Thanks guys!