1D0-635 · Question #38
Which special character in JavaScript is interpreted as a backslash?
The correct answer is C. \\. See the full explanation below for the reasoning.
Question
Which special character in JavaScript is interpreted as a backslash?
Options
- A\b
- B/
- C\
- D\r
How the community answered
(39 responses)- A5% (2)
- B3% (1)
- C79% (31)
- D13% (5)
Community Discussion
5The answer is C, the double backslash. Here is the sticky image I use: picture a backslash so shy it needs a bodyguard, and the only bodyguard it accepts is another backslash. One backslash alone is just a signal flare telling JavaScript "escape sequence coming," so it disappears into the string like a ghost. You need TWO of them side by side, and JavaScript reads that pair and hands you back exactly one real backslash character. Remember it with the phrase "Doubled to Survive," or just think D.T.S. every time you see \\. The other choices are traps: \b is your Backspace buster, \r is your Return runner (carriage return), and \/ is just a forward slash wearing a costume. None of those wear the backslash crown. Only \\ earns it, two marks walking in so one mark comes out.
The phrasing "interpreted as a backslash" trips people up because every option here is an escape sequence, so it feels like a trick, but the question is specifically asking which escape sequence represents a literal backslash character, and that is C, the double backslash, where the first backslash escapes the second to produce a single backslash in output. B is the tempting wrong turn since forward slash does not need escaping in most contexts, making that sequence a bit of a red herring in the spec.
The double backslash point is solid, though worth adding that in raw strings like Python's r-strings the rules flip entirely and a single backslash is literal, which catches a lot of people when they move between contexts.
C is the one, but do you know why you'd ever need to escape a backslash in a real string?
The answer is C, the double backslash. The reason it trips people up is that the backslash itself is the escape character in JavaScript, so you need a second one to tell the interpreter you actually mean a literal backslash and not the start of a special sequence. Option A is a backspace, D is a carriage return, and B is just a forward slash that does not need escaping at all but some people write it that way in regex. My senior showed me a trick, he said think of it like quoting a quote mark, you have to introduce it with something so the language knows you are being literal. On my actual exam I second-guessed myself on this one because the way the options rendered made the double backslash look almost identical to a single one at a glance on the screen. I sat there for probably a full minute before I remembered what my senior said and locked in C. Came out and checked it after, and yeah, C is right every time. If you see a backslash question, just ask yourself whether the character is escaping something else or whether it needs to escape itself, and it clicks into place.