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

Template Literal VS Variable?

https://w.trhou.se/3alz759am4

Template Literals makes my code easier to write, but I'm concerned, does the last function in the file remove the need for variables? (Line 20) as opposed to (Line 2 & 3)?

1 Answer

Hi jason7,

In this case, the second function does remove the need to declare a variable to hold the area or one to hold the area to two decimal places.

You could even write the function like this:

// this is called an arrow function
const getArea = (width, height, unit) => `${(width * height)}.toFixed(2) ${unit}`;

Programmers use both depending on the goals. The more characters in a file generally means the longer it takes the interpreter to run that file (so it's a good thing to refactor when you can). But sometimes writing code the first way you have it in your workspace means making your code easier to understand for your future self and for others.

It's easy to refactor code to the point of illegibility. So again, both are used depending on the situation.

Thanks for your time, I see your point it depends on the situation (starting to like arrow functions, so cool) thanks again.