0d: 00h: 00m: 00s

🎁 Get 3 extra months of learning — completely FREE

That's 90 extra days to master new skills, build more projects, and land your dream job.

Become a PRO
Background Image

🎁 Get 3 extra months of learning completely FREE

Become a PRO
James Blasingim
PRO
last month
Jamescountry asked

How can I show a dollar sign before a number in JavaScript to make the output look nicer?

Abhay Jajodia
Expert
last week
Abhay Jajodia answered

Hello James, really nice question.

In JavaScript, the easiest way to make the output look cleaner with a dollar sign is to format it as a string. A very common way to do that is with a template literal (using backticks `):

let costPrice = 25;
let sellPrice = 35;

let profit = sellPrice - costPrice;

console.log(`Profit: $${profit}`);

What’s happening here:

  • The backticks let you write a string with embedded expressions.

  • ${profit} is replaced by the actual value of profit.

  • The $ before it is just a normal character in the string, so it shows up exactly as you’d expect.

You could also do it with string concatenation:

console.log("Profit: $" + profit);

Both work, but template literals are usually cleaner and easier to read, especially as the text gets longer.

If you have further questions, I'm here to help.

JS
This question was asked as part of the Learn JavaScript Basics course.