Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Quickstart Data Types and Variables Creating Variables

Kevin Ohlsson
Kevin Ohlsson
4,547 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
218,692 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,547 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
218,692 Points

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

Kevin Ohlsson
Kevin Ohlsson
4,547 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
218,692 Points

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