JavaScript Strings: Basic Information

A JavaScript string is a series of characters enclosed in single or double quotes. JavaScript strings are exactly the same whether they are inside single or double quotes. Unlike some other languages, there is no difference in the handling of escape sequences.

You can enclose a single-quoted string inside a double-quoted string, and vice versa. A double-quoted string inside a single-quoted string is useful for HTML with attributes:

var str = '<a href="page.html">Link</a>';

You can include a single quote or apostrophe within a single-quoted string as long as you escape it, that is, place a backslash in front of it:[1]

var str = 'There\'s more to the story.';

Long Strings and Line Breaks

If you insert a carriage return in a string in JavaScript, an error will be triggered:

var str = 'This is an example JavaScript string 
    with a line break.';
// SyntaxError: unterminated string literal

You can use the plus sign to concatenate a series of strings across multiple lines, as we demonstrate here:

var str = 'This is an example JavaScript string ' +
    'with a line break.';
console.log( str );
// This is an example JavaScript string with a line break.

ECMAScript 5 introduces the backslash character (\) as another way to break a long string:

var str = 'This is an example JavaScript string \
    with a line break';
console.log( str );
// This is an example JavaScript string     with a line break.

Notice that with this approach the extra spaces used to indent the second line are retained, as shown in the output of console.log.

String Object Properties and Methods

A string is a primitive data type, yet JavaScript provides a String class with properties and methods for use with string literals and variables. Dot syntax is used to apply these properties and methods to string instances. For example, the length property returns the number of characters in a string. It is applied as follows:

var str = 'hello';
var len = str.length; // 5

String methods do not modify the strings they are applied to. Instead they return new strings. We demonstrate by applying the toUppercase method to an example string:

var str = 'hello';
var str2 = str.toUpperCase();
// display string returned by toUppercase
console.log( str2 ); // HELLO
// display original string
console.log( str ); // hello

As you can see, the toUpperCase method converts all the characters of the string to uppercase. What if we want to convert just the first character to uppercase?

JavaScript does not provide a large number of string methods, so when you want to accomplish something a little more complex, you have to be creative. Since the string methods return modified strings, we can chain them, applying subsequent methods to the results of the previous.

To convert the first letter of a string to uppercase, we combine the charAt, toUppercase, and slice methods as follows:

var str = 'hello';
var str2 = str.charAt(0).toUpperCase() + str.slice(1);
console.log( str2 ); // Hello

More JavaScript String Methods

The JavaScript Strings Tutorial Overview lists the string methods, and pages where they are described and demonstrated.

Back to top

  1. JavaScript also supports other escape sequences, including \n (new line), \t (the tab character), and \\ (the literal backslash character). See the complete list at MDN. ^