JavaScript JavaScript Basics Working With Numbers The Mad Libs Challenge Revisited

Sean Daniel
Sean Daniel
4,811 Points

What exactly are these apostrophes doing in this string?

I understand how you use quotation marks to make strings and do not put them on variables.

var questions = 3;
var questionsLeft = ' [' + questions + ' questions left]';
var adjective = prompt('Please type an adjective' + questionsLeft);`

The prompt: Please type an adjective [3 questions left]

What exactly are these apostrophes doing in this string?

1 Answer

Hey Sean,

So in JavaScript, Apostrophes or Quotation marks can be interchanged to create a string, and can sometimes depend on company or personal preferences.

In this function specifically, the apostrophes are there to break the equation between string and variable. So in

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

The beginning of the string is closed off with apostrophes at:

' [' 

The middle of the string is applied from the questions variable, and the end of the string is closed off and added with:

' questions left]'

All of these added together create the full string of [3 questions left].

This one was a little confusing because of the square brackets being included in the prompt, but its all just part of the string output. (these could all be done with quotations instead of apostrophes too)

Hope this helps!

Dylan

Sean Daniel
Sean Daniel
4,811 Points

Thank you Dylan! Problem solved.

Michael Darcy
Michael Darcy
11,431 Points

I gave you a +1. I was completely misreading what the value for the questionsLeft variable was doing. It had initially looked like there was a string contained within a string.