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 trialKevin Ohlsson
4,559 PointsHow to call function? Combinding template literal and arrow functions
Hello
How do i make my code into a function that i can call?
I used template literals and arrow functions in this code.
https://teamtreehouse.com/library/getting-started-with-es2015-2
function:
let myName = "Kevin"
const lastName= "Ohlsson"
const message = () => {
`
${myName} is ${lastName}
`
console.log(message)
}
message()
original:
let myName = "Kevin"
const lastName = "Ohlsson"
const message = `${myName} is ${lastName}`;
alert(message)
1 Answer
Steven Parker
231,236 PointsYou were close, but you need to assign your string to a variable, and log that variable instead of the function itself:
const message = () => {
const msg = `
${myName} is ${lastName}
`;
console.log(msg);
};
Kevin Ohlsson
4,559 PointsKevin Ohlsson
4,559 PointsThank you Steven!
I guess i tried to log the function itself and the string was lost in memory because it was not temprarily assigned to any variable.
Iยดll practise more :)
A translation of my first code snippet to a "normal" function would be:
Here is the code again, but now using your example.
Makes it easy to see the misstake
Cheers!
Steven Parker
231,236 PointsSteven Parker
231,236 PointsAnd if I may suggest, also practice the good habit of ending statements with semicolons.
Kevin Ohlsson
4,559 PointsKevin Ohlsson
4,559 PointsGood eye! I keep missing them! I need to come up with a mantra about semicolons so i remember them.
Steven Parker
231,236 PointsSteven Parker
231,236 Points"Every JavaScript statement gets a semicolon abatement"
"Make each statement your friend, put a semicolon at the end"
Kevin Ohlsson
4,559 PointsKevin Ohlsson
4,559 Pointsperhaps only some ;)