Name and city exercise - solution for ${} problem


#1

As the previous topic about this has been closed I’m starting a new one in case anyone is stuck the same way I was.

The exercise is this

I couldn’t get the ${myName} to work. Everything up to that point worked. I was able to create variables, but wasn’t able to use the ${ command. I tried a lot of stuff on my own, but in the end I took the “get code” option and the correct code was posted in the field.

let myName = 'Nitin';
let myCity = 'London';

console.log(`My name is ${myName}. My favorite city is ${myCity}.`);

console.log('My name is ${myName}. My favorite city is ${myCity}.');

Now the first console line is the correct, and the second one was written by me. The first one works, but the second one doesn’t. I logs, yeah, but it logs as a string. I noticed in when I tried typing it side by side in a text editor that the “quotation marks” where different. The first console line above uses the ` sign, not the ’ apostrophe or the " quotation mark I had tried.

On my keyboard it’s the same as the tick (for example in café <-) so shift+tick and then space and it’ll print a ` backtick (as on top of è). If one doesn’t use the it won’t activate the ${ command and just print the whole thing as a string.

That is what they mean when they mean “use backticks” in the instructions. As that term was new to me I figured they meant the apostraphe or the quotation mark, so this is a reminder to newbies and myself to properly read the instructions.

Edit: Apparently that same symbol is used to edit the text in this forum so… yeah. Hope that it’s understandable at least.

Edit 2: Noticed spelling errors…


#2

On most standard qwerty keyboards the backtick is above the Tab key and shares a key with the tilde (this one ~) button.

Your second console line logs as a standard string because apostrophes are just interpreted as string enclosures and so it outputs everything between them without trying to interpolate anything automatically.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.