Mad Libs Word Blanks


I am currently on the Madlibs Word Blanks exercise
However, in the “Get a hint” section of the forum, several questions I have are still not answered.
I was hoping you guys could clear up some confusion I have:

  1. Why should I use += instead of = ? I know we used += in a previous exercise, but it is still unclear to me why this is actually supposed to be easier or handier. Any explanation as to why this is would be great.

  2. Why should I use "+myAdjective+" to add words together in the string? Why can’t I simply do the following:

result = ' "My " + myAdjective + myNoun + myVerb + " very " + myAdverb. ';

Thanks a lot. I am really curious for the rationale behind this. (also why we have to use something we haven’t learned about before, namely the +variablename+ thing. (also, what is the name of this, because I can’t even find it in any js reference).

I’m not sure how += can be useful in that example, since the value in result is just an empty string, and = would work equally well. A matter of taste perhaps?

It will result in a malformed string. There wouldn’t be a space between the adjective, noun and verb, and you’ll get this:

'My bigdogran very quickly.'

Thank you for your quick and helpful reply.

So, does this mean that the +myAdjective+ simply adds a space, whereas the +myAdjective does not?
Is that the difference between the +variable+ and +variable?

If so, why use +myAdjective+ instead of +"myAdjective "? Is there a reason for this, or just a matter of preference or convention?

Again, thanks a lot!

The string that’s contained in myAdjective contains no spaces in it. You have to fill in the spaces yourself to make a proper sentence.

"My " + myAdjective + " " + myNoun + " " + myVerb + ...

+myAdjective+ is very different from +"myAdjective ". The former substitutes the value contained in myAdjective and puts it in the result string. The latter literally adds the string "myAdjective " to the result.


// if `myAdjective` contained `"big"` and myNoun contained `"dog"`
result = "My " + myAdjective + " " + myNoun
// "My big dog"
result = "My " + "myAdjective " + myNoun
// "My myAdjective dog"

In other words, characters between the quote marks are treated literally.

1 Like

Thank you!
This really clear things up for me.