0

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?

k.ken
  • 4,973
  • 5
  • 23
  • 21

3 Answers3

2

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)\\]"

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 1
    JavaScript is a language of the C family? – Dennis Traub Dec 12 '12 at 15:51
  • 2
    @DennisTraub Its syntax, yes, just like java, Go, C#, etc. – Denys Séguret Dec 12 '12 at 15:52
  • I would say "language with C style syntax". – Alnitak Dec 12 '12 at 15:52
  • 2
    It might somewhat look like a C-based language, but that doesn't make it one. In fact I only started to understand JavaScript once I recognized it's not C-based. – Dennis Traub Dec 12 '12 at 15:54
  • They're just somewhat related syntaxwise. A lot of the concepts don't translate very well in either direction, but if you know C syntax, it goes a long way toward learning the basic syntax of a bunch of other languages. The concepts in JS, though, came from a whole different place (Scheme, Self, and a few others not even close in syntax). – cHao Dec 12 '12 at 15:54
  • @DennisTraub I edited to make it clearer but it's not uncommon to speak of the C-family languages when speaking of languages whose syntax is inherited from C. – Denys Séguret Dec 12 '12 at 15:56
  • @DennisTraub I feel that the mindset of thinking "oh I know, that's just like in " mostly gets you into trouble. I try to understand the semantics of each language anew without letting other languages cloud my judgement. – phant0m Dec 12 '12 at 15:56
  • If you talk about syntax, I think all languages has implemented escape character, and they're use same character, the `\` – Alfian Busyro Dec 12 '12 at 15:58
  • @phant0m but it can help you start faster. You probably don't know Go but just looking at [this](http://stackoverflow.com/questions/12462377/selecting-a-function-from-a-list-of-functions-in-golang/12462668#12462668) you can get an idea of the structure of the program. – Denys Séguret Dec 12 '12 at 15:59
  • @dystroy Sure, I agree on that. – phant0m Dec 12 '12 at 16:04
  • @arufian: Most, but not all. VB, for example, doubles quotes and (AFAIK) doesn't otherwise use escape characters. And apparently Delphi uses `#xx` (where `xx` is a numeric character code). – cHao Dec 12 '12 at 16:07
1

Backslash characters are special.

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

"\\[(tan(w)\\]"
Alnitak
  • 334,560
  • 70
  • 407
  • 495
1

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. \\

I Hate Lazy
  • 47,415
  • 13
  • 86
  • 77