Ravi Kumar
Ravi Kumar

Reputation: 23

JavaScript: Why backslash placed at the end of the string breaks the JavaScript code?

But backslash placed anywhere else doesn't breaks the script.

eg: abcd\ will break the script,
while a\bcd will not break the script.

Upvotes: 0

Views: 716

Answers (3)

crollywood
crollywood

Reputation: 563

My guess would be that you "escaped" quotes, that is, if you make string like this

var a = "abcd\"

it will escape that quote and practically leave your string unclosed, which breaks the script.

You could do double backslash in order to put the backslash on the end if that is what you wanted.

var a = "abcd\\"

Upvotes: 2

Maxim Gritsenko
Maxim Gritsenko

Reputation: 2592

In the string backslash is an escape symbol. When you write:

"abcd\"

you string literal is never finished, for the quotes are escaped. You need to write:

"abcd\\"

Upvotes: 3

Shocklo
Shocklo

Reputation: 467

because javascript escapes with backslash, meaning that it will try to escape the next character, but since there is none, its fails. In order to output a literal backslash, escape it, so your output should be abcd\\

Upvotes: 0

Related Questions