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

Angelica Islas
Angelica Islas
4,082 Points

How do I solve this challenge?

script.js
greet('cool coders');

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

3 Answers

Denis Omerovic
Denis Omerovic
2,377 Points

You must declare function first, otherwise, it won't work.

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

greet('cool coders');

Gemini Brain
Gemini Brain
13,817 Points

Exactly as Denis mentioned. If you create a function declaration then it does not matter if you call the function before or after initialization. It will work. However, in case of an arrow function or a function expression the function initialization has to be before the call otherwise the console throws an error and it won't work.

Angelica Islas
Angelica Islas
4,082 Points

I tried to run this in JS console and got the following message. What is this wrong?

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

greet('cool coders'); VM34:1 Uncaught SyntaxError: Unexpected token '{'

Gemini Brain
Gemini Brain
13,817 Points

Hi, you apparently omitted the quotation marks. Try this:

const greet = (val) => {
return `Hi, ${val}!`;
}
greet('cool coders');
Denis Omerovic
Denis Omerovic
2,377 Points

You are probably missing backticks ``

const greet = (val) => {
    return `Hi, ${val}!`;
}
greet('cool coders');