0

when I try to write such JS statement

var a = "\images\avatars\";

I am getting an error SyntaxError: Unexpected token ILLEGAL

This is definitely because of \ sign, but I need them. So how can I make this string safe?

PS. escape, encodeURI doesn't help

0

3 Answers 3

3

Double them:

var a = "\\images\\avatars\\";

You'll always need to double a backslash if you're including it in a string literal. You'll have to do something similar to embed the same kind of quotes in a quoted string:

var a = 'Don\'t be cruel';

The backslash serves as an escape sequence.

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

Comments

1

Escape them:

var a = "\\images\\avatars\\";

Why do you need backward slashes rather than forward slashes, by the way?

Comments

1

Do this instead.

var a = "\\images\\avatars\\";

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.