0
assert(JSON.stringify(searchForMatches(searchData,['cats']))==='["catcode.io","catgifs.co"]',"The result should be '[\"catcode.io\",\"catgifs.co\"]'");

Why do I need the use of \ at the end multiple times? It throws an error otherwise. Is it specific to JSON.stringify and/or when/how should I go about about using it in future instances?

1
  • 3
    \ is signifying you are escaping characters. Commented Apr 9, 2018 at 17:20

2 Answers 2

1

\ is an escape operator. It prevents the language from parsing the next char as syntax relevant, so without it your string stops at the next " after the first " which starts the string.

Sign up to request clarification or add additional context in comments.

Comments

1

The backslash is a JavaScript operator used within a string for escaping a special character. In your case it is escaping a quotation. See this example:

// in this case it escapes the " symbol because that would end the string.
var x = "\""; 

This is the same as:

// in this case you don't need to escape it because 
//   the string begins with the apostrophe instead of the " character
var x = '"'; 

In both cases console.log(x); will print a single quote character.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.