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

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
221,322 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.