user160820
user160820

Reputation: 15220

What is the best possible way to pass a Mulitline String as function argument?

What will be the Best practice to pass Multiline String as a Javascript function's argument.

Upvotes: 0

Views: 336

Answers (2)

T.J. Crowder
T.J. Crowder

Reputation: 1075159

JavaScript strings can contain newline characters, so you don't have to do anything special. (Edit Actually, I suppose you do — you have to use an escape sequence; that's covered below.) Example:

var s = "This is line 1\nThis is line 2\nThis is line 3.";
alert(s);

...alerts

This is line 1
This is line 2
This is line 3

The full syntax of the escape sequences in JavaScript strings is (of course) in the specification, but here's a brief (probably incomplete) rundown:

  • \n - newline
  • \r - carriage return
  • \t - tab
  • \v - vertical tab
  • \b - backspace
  • \f - formfeed
  • \u0100 - Unicode character 256 (e.g., the value is in hex, four digits)
  • \xfd - Unicode character 253 (e.g., the value is in hex, two digits)
  • \\ - backslash
  • \" - double quote (useful when you're using double quotes to delimit a string literal)
  • \' - single quote (useful when you're using single quotes to delimit a string literal)

Upvotes: 1

VoodooChild
VoodooChild

Reputation: 9784

You can pass it a variable!

Upvotes: 1

Related Questions