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 Quickstart Data Types and Variables Creating Variables

Kevin Ohlsson
Kevin Ohlsson
4,559 Points

How 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:

scripts.js
let myName = "Kevin"
const lastName = "Ohlsson"

const message = `${myName} is ${lastName}`;

alert(message)

1 Answer

Steven Parker
Steven Parker
231,275 Points

You 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
Kevin Ohlsson
4,559 Points

Thank 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:

messageOne() {
    `${myName} is ${myAge}`
    console.log(messageOne)
}

messageOne()

Here is the code again, but now using your example.

function messageTwo() {
    const msg = `${myName} is ${myAge}`
    console.log(msg)
}

messageTwo()

Makes it easy to see the misstake

Cheers!

Steven Parker
Steven Parker
231,275 Points

And if I may suggest, also practice the good habit of ending statements with semicolons. :wink:

Kevin Ohlsson
Kevin Ohlsson
4,559 Points

Good eye! I keep missing them! I need to come up with a mantra about semicolons so i remember them.

Each javascript stolon gets swollen and ends with a semi-colon;

Steven Parker
Steven Parker
231,275 Points

"Every JavaScript statement gets a semicolon abatement"
"Make each statement your friend, put a semicolon at the end" :smirk: