Free JavaScript course. Sign Up for tracking progress →

JavaScript: Quotation marks

'Hello'
'Goodbye'
'G'
' '
''

Which of these five items are strings?

The first two are clearly strings, we've already worked with similar constructions and mentioned that strings are sets of characters.

Any single character between parentheses is a string. The empty string '' is also a string. So everything inside the quotation marks can be considered a string, even if it is a space, one character, or no characters at all.

In previous lessons, we enclosed strings in single quotes, you can also use double quotes:

// Coding airbnb standard recommends
// using single quotes where possible

console.log('Dracarys!');

Imagine you want to print a string: Dragon's mother. The apostrophe before the letter s and a single quote are the same symbols. Let's print it:

console.log('Dragon's mother');
// Uncaught SyntaxError: missing ) after argument list

This program won't work. For JavaScript, the string begins with a single quote and ends after the letter n. Then comes some characters: s mother without quotes, which are not a string. And then there is one quote opening a sting which is never closed: ');. This code is syntactically incorrect (you can see it by the way the code is highlighted).

It's a good idea here is to use double quotes. This version of the program will work correctly:

console.log("Dragon's mother");

Now the interpreter knows that the string begins with a double quote, so it should end with a double quote too. And the single quote inside has become the part of the string.

It works the other way too. If you want to use double quotes inside a string, you should enclose the string in single quotes. And the number of quotes in a string doesn't matter.

Now, what if we want to create a string like this?

Dragon's mother said "No"

There are single and double quotes here. What can we do in this case? You need to somehow tell the interpreter to consider each quote as a part of the string, and not as its beginning or an end.

To do this, you have to use an escape character. In our case, the character that starts and ends a string is either a single or a double quote, depending on the part of the code. Use a backslash \ before the character you want to escape.

// Only " is escaped, because in this code 
// double quotes have a special meaning

console.log("Dragon's mother said \"No\"");
// => Dragon's mother said "No"

Look closely: we had to use \ for double quotes to escape them, and not for the single quote (apostrophe) because the string is written in double quotes. If the string were written in single quotes, the escape character would be used before the apostrophe, not before double quotes.

// \ won't be printed if it is followed by a normal character,
// not a special one

console.log("Death is \so terribly final");
// => Death is so terribly final

But what if you want to print the backslash? Just like any other special symbol, it escapes using a backslash too.

console.log("\\");
// => \

Self-test: what will be printed?

console.log("\\ \\ \\\\ \\\ \'\"");

Instructions

Write a program that prints:

"Khal Drogo's favorite word is "athjahakar""

The program should print the exact phrase. Note the quotes at the beginning and the end of the phrase:

"Khal Drogo's favorite word is "athjahakar""
The exercise doesn't pass checking. What to do? 😶

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
In my environment the code works, but not here 🤨

Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.

My code is different from the teacher's one 🤔

It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.

In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.

I've read the lessons but nothing is clear 🙄

It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.

By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.

Tips


If you got stuck and don't know what to do, you can ask a question in our huge and friendly community