Constructing Strings with Var

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

Your code so far


// Only change code below this line
var myName ="Gabe";
var myStr ="my name is" + "myName" + and I am well"!;

Console.Log(myStr)

Your browser information:

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

Challenge: Constructing Strings with Variables

Link to the challenge:

Hi, be careful of the placements of your quotation marks. When a set of quotation marks enclose any string it becomes s string literal and is no longer a variable. This also means any string not enclosed by double quotes, Javascript is going to try to interpret as syntax.
e.g.

var name="Sam" //name variable assigned the string "Sam"
var name2=Sam //Javascript looks for a variable named Sam to assign to name2
1 Like

To pass this particular challenge, you need to use a string variable while building the myStr variable.

myName is a variable here because you have used the var keyword in front of it. Here, myName variable has been assigned with a string value which is supposed to be a name.

Now, if you use this myName variable between 2 strings then you have to make sure that you don’t enclose your myName variable with quotation marks unlike your other 2 strings. If you do end up putting quotation marks before and after your myName variable then myName will turn into a string literal inside myStr variable thus making myStr unable to use the string value that you have stored inside your myName variable. Therefore, you will see something like this in the console:

my name is myName and I am well!

In addition, I can see that the last string inside your myStr variable is missing a quotation mark at the beginning. Also, the exclamation mark should be inside your last string’s quotation marks since it is part of your string as well.

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