k.ken
k.ken

Reputation: 5413

Javascript parameter

I noticed that when I pass a string like this \[(tan(w)\] as a parameter to a function, the argument when printed in the function I passed it into is [(tan(w)]. Why would the slashes get stripped?

Upvotes: 0

Views: 82

Answers (3)

I Hate Lazy
I Hate Lazy

Reputation: 48761

In a string literal, the \ has special meaning. It means that you're starting an escape sequence meant to represent some character.

If the escape sequence actually has a specified meaning, the new character is substituted for the entire sequence. If not, the slash is just removed.

The escape sequence to include a literal backslash in the resulting string is a backslash followed by another backslash. \\

Upvotes: 1

Alnitak
Alnitak

Reputation: 339796

Backslash characters are special.

If you want to pass one in a string, and have it preserved, you have to pass two:

"\\[(tan(w)\\]"

Upvotes: 1

Denys Séguret
Denys Séguret

Reputation: 382122

The slash in a string in any language whose syntax is inherited from C is used to escape other characters. For example if you want to put a double quote (") in your string, you use \"

To put a slash in a string, you have to put a double slash : "\\[(tan(w)\\]"

Upvotes: 2

Related Questions