Why does the hint tell me to use ${}?

Hi I’m doing the Kelvin Weather project for Javascript right now.

This is my code that works:

// They told me to write a comment here
const kelvin = 0

// Celsius is 273 degrees less than Kelvin
let celsius = kelvin - 273

// The lil equation to convert Celsius to Fahrenheit
let fahrenheit = celsius * (9/5) + 32;
// Rounds down to lower decimal
fahrenheit = Math.floor(fahrenheit)

// Newton equation for extra practice
let newton = celsius * (33/100)

console.log('The temperature is ' + fahrenheit + ' degrees Fahrenheit.')

To check my work, I checked the hints, and the hints say it should look like this:

console.log(`The temperature is ${fahrenheit} degrees Fahrenheit.`);

When I use that instead, I get the error:

SyntaxError: missing ) after argument list

If I use $(fahrenheit) instead of ${fahrenheit} I get the error:

$ is not defined.

Am I missing something here? Why would I want to use this if the way I have it works fine?

Both ways technically work, but Codecademy is trying to encourage you to use string interpolation which uses backticks (`) and ${} to insert expressions into strings. In this case, you’ll have to use string interpolation to continue, but I would also encourage you to use string interpolation because it tends to be easier to read and is overall less cumbersome to type, especially if you need to add a lot of expressions right next to each other in a given string. For example:

let dayOfWeek = 'Sunday'
let dayOfMonth = '12th'
let month = 'July'
let year = '2022'
console.log('The current day is ' + dayOfWeek + ', ' + month + '  ' + dayOfMonth + ' of ' + year '.')
// versus
console.log(`The current day is ${dayOfWeek}, ${month} ${dayOfMonth} of ${2022}.`)
// both log 'The current day is Sunday, July 12th of 2022.'

Both of those print out the same string, but I think the latter is way easier to write and read.

1 Like

Oh, I didn’t even notice the backticks which is why I was getting that error, and yes, this is much easier to type out. Thank you!

1 Like