Strings are sequences of characters, in other words texts.

Since JavaScript does not have a separate character data type, characters are represented as Strings having just one letter.

var char_c = "c";

You can use either single or double apostrophes for new strings.

var s = 'hello';
var t = "hello"; 

You can also split strings over multiple lines in code using a backslash immediately followed by a newline. This will not insert a newline when you output the string, so you have to use a “\n” if you want a newline to be output. Splitting strings over multiple lines carries some degree of risk, since it is easy to mess up the trailing newline.

var s = 'this string \
is formatted across \n multiple lines \
in the code.'

The aformentioned string might be output, say in a PRE-tag as:

this string is formatted across 
 multiple lines in the code.';

Like in C# strings in JavaScript are immutable. So for instance each time you concatenate two string you are in fact creating a new string.

var s = "hello ";
s = s + " world";   // s is now a reference to a different String object than before
Advertisements