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

shannon sivertsen
shannon sivertsen
8,258 Points

Can someone help me understand this line of code?

token-${index}-${owner.id}

I'm not understanding what the -${} is doing.

Thanks!

2 Answers

Steven Parker
Steven Parker
229,582 Points

Assuming that is enclosed in accents (`), it's a template string. Each identifier along with the braces and leading dollar sign will be replaced by the string representation of the value that the identifier contains. This process is called interpolation.

For example, if the variable "index" contains 3, and "owner.id" is "joe", then `token-${index}-${owner.id}` would be interpolated into the string "token-3-joe".

shannon sivertsen
shannon sivertsen
8,258 Points

ah thanks! I wasn't sure if the dash was part of the code or part of the string. Makes more sense seeing the outcome. Thank you!

The dollar sign and curly brackets indicate a placeholder in a template literal. See description here and expressions here for further explanation. In the code you provided the values for index and owner.id will be concatenated with the rest of the text.