Issue with String interpolation


#1

https://www.codecademy.com/courses/learn-javascript-introduction/lessons/variables/exercises/string-interpolation-ii?action=lesson_resume&course_redirect=introduction-to-javascript

Output:
“My name is ${myName}. My favorite city is ${myCity}.”

Code:



let myName = "Martin";
let myCity = "New York";
console.log("My name is ${myName}. My favorite city is ${myCity}.")


Can someone tell me what I’m doing wrong here?


#2

to use es6 string interpolation with ${variable} the string should be enclosed in back-ticks, not quotation marks, as you can see in the example:

let myPet = 'armadillo'
console.log(`I own a pet ${myPet}.`)
// Output: 'I own a pet armadillo.'

see? back-ticks are used to enclose the string


#3

I tried with what I thought was a backpack ( ´ ) but it kept dissapearing… I found out that Norwegian keyboards dont actually have backpacks apparently… Thats fun…

Edit: Thanks though :slight_smile:


#4

backpack? that is something differently internally

you can always try to set your keyboard layout to en-US or something, or just copy the bactick, here is one for you:

`

or google how you get it on a norwegian keyboard


#5

Im unfortunately using a Mac atm, and its autocorrecting everything -_- I meant a backtick… But yeah, for now ill just copypaste it :slight_smile:


#6

Mac is a bit tricky sometimes, because it inserts smart quotes which is not ideal for programming, you might need to disable that.

macs must have a backtick? Is this your keyboard layout:

uhm… your backtick seems to be left of the backspace key, in your case its not positioned left of the 1 which is common

given its at the top, it seems you need to press shift + that key


#7

Ah, I just pressed it normally, so I guess I got a normal tick… Still wierd how it dissapeared though, but thanks :slight_smile:


#8

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