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

heather shockney
heather shockney
2,865 Points

Using visual studio for node.js and variables in strings not printing out

I am trying to run some really basic code. I operated in Visual Studio with Ruby and I am trying to run

function printMessage(username, badgeCount, points) {
    const message = '#{username} has #{badgeCount} total badge(s) and #{points} points in Javascript';
    console.log(message);
}
printMessage("Chalkers", 100, 2000000);

the result is:

> node test1.js
#{username} has #{badgeCount} total badge(s) and #{points} points in Javascript

I've tried replacing the #'s with $'s, and the single quotes with double quotes. If anyone could help me I'd appreciate it. I did notice if I do console.log(%s, username) it prints out Chalkers correctly, but that is not the way he is teaching it and I don't want to stray from his methods.

and for an update, I tried the code in Treehouse, and Sublime, and I am getting the same results. Is there a simple error in there somewhere I am missing?

1 Answer

Samuel Ferree
Samuel Ferree
31,722 Points

To interpolate values into javascript template literals, you want to define you strings with backticks, and place the variables in ${}, like so:

function printMessage(username, badgeCount, points) {
  const message = `${username} has ${badgeCount} total badge(s) and ${points} points in JavaScript.`;
  console.log(message);
}

printMessage('Chalkers', 100, 2000000);
heather shockney
heather shockney
2,865 Points

Thank you very much. It wasn't single quotes or double quotes but the hyphen key. I would have never have saw that. I need glasses very badly. Thanks again!!