Basic JavaScript - Constructing Strings with Variables

Tell us what’s happening:
Describe your issue in detail here.

Hi, I passed this lesson, but my question is this. Just a few lessons before this it is said that if you use quotations twice in a string the program will end, which is why its necessary to use a backslash. So why is that not the case here aswell?

I had initially made my solution as such:
const myName = “Junaid Ahmed”;
const myStr = “My name is " + myName + " and I am well!”;

But it said this is wrong, and then it worked without the backslashes. Could someone please explain why, Thanks!

Your code so far

// Only change code below this line
const myName = "Junaid Ahmed";
const myStr = "My name is " + myName + " and I am well!";

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Constructing Strings with Variables

Link to the challenge:

The backslash is in case you want the quote inside the string, here you want to concatenate the value of myName instead
You can check

const myName = "Junaid Ahmed";

const myStr1 = "My name is \" + myName + \" and I am well!";
console.log(myStr1);  // My name is " + myName + " and I am well!
const myStr2 = "My name is " + myName + " and I am well!";
console.log(myStr2); // My name is Junaid Ahmed and I am well!

Hi, thank you.
But why does the string not stop once it reaches the second quotation? should it not close at that point?

like here:

const myStr2 = “My name is " + myName + " and I am well!”;
console.log(myStr2); // My name is Junaid Ahmed and I am well!

When it hits that second quotation, right after “is”, why doesnt it end and then just print: //My name is

Because the + sign is a concatenation method for strings.

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