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 trialja5on
10,338 PointsTemplate Literal VS Variable?
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
Brandon White
Full Stack JavaScript Techdegree Graduate 35,771 PointsHi 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.
ja5on
10,338 Pointsja5on
10,338 PointsThanks for your time, I see your point it depends on the situation (starting to like arrow functions, so cool) thanks again.