JavaScript JavaScript Basics Working With Numbers The Mad Libs Challenge Revisited

Tre Bu
Tre Bu
1,521 Points

Confusion on the '[' + questions + ' questions left]'

Could someone explain why you have the apostrophes go where they are here?

Do the ' outside the brackets represent the whole string and the ' inside the brackets convert the 3 to a string and add spacing?

2 Answers

Steven Parker
Steven Parker
177,495 Points

The apostrophes (also known as "single quotes") enclose literal strings. The brackets are entirely inside the strings and have no meaning to the code. The syntax coloring provided by the formatter may help clarify this:

questionsLeft = '[' + questions + ' questions left]';

Note that the strings are a different color than the code. The system automatically coerces (converts) the number in "questions" into a string when it is combined with the other strings by the + operator.

Tre Bu
Tre Bu
1,521 Points

I understand now, thank you so much for the help!

Best

Steven Parker
Steven Parker
177,495 Points

Tre Bu — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!