< JavaScript
Previous: Numbers Index Next: Dates

A string is a type of variable that stores a string (chain of characters).


Basic useEdit

To make a new string, you can make a variable and give it a value of new String().

var foo = new String();

But, most developers skip that part and use a string literal:

var foo = "my string";

After you have made your string, you can edit it as you like:

foo = "bar";		// foo = "bar"
foo = "barblah";	// foo = "barblah"
foo += "bar";		// foo = "barblahbar"

A string literal is normally delimited by the ' or " character, and can normally contain almost any character. Common convention differs on whether to use single quotes or double quotes for strings. Some developers are for single quotes (Crockford, Amaram, Sakalos, Michaux), while others are for double quotes (NextApp, Murray, Dojo). Whichever method you choose, try to be consistent in how you apply it.

Due to the delimiters, it's not possible to directly place either the single or double quote within the string when it's used to start or end the string. In order to work around that limitation, you can either switch to the other type of delimiter for that case, or place a backslash before the quote to ensure that it appears within the string:

foo = 'The cat says, "Meow!"';
foo = "The cat says, \"Meow!\"";
foo = "It's \"cold\" today.";
foo = 'It\'s "cold" today.';

Properties and methods of the String() objectEdit

As with all objects, Strings have some methods and properties.


The concat() function joins two strings.

var foo = "Hello";
var bar = foo.concat(" World!")
alert(bar);	// Hello World!


Returns the length as an integer.

var foo = "Hello!";
alert(foo.length);	// 6

replace(text, newtext)Edit

The replace() function returns a string with content replaced. Only the first occurrence is replaced.

var foo = "foo bar foo bar foo";
var newString = foo.replace("bar", "NEW!")
alert(foo);		// foo bar foo bar foo
alert(newString);	// foo NEW! foo bar foo

As you can see, the replace() function only returns the new content and does not modify the 'foo' object.

slice(start[, end])Edit

Slice extracts characters from the start position.

"hello".slice(1);	// "ello"

When the end is provided, they are extracted up to, but not including the end position.

"hello".slice(1, 3);	// "el"

Slice allows you to extract text referenced from the end of the string by using negative indexing.

"hello".slice(-4, -2);	// "el"

Unlike substring, the slice method never swaps the start and end positions. If the start is after the end, slice will attempt to extract the content as presented, but will most likely provide unexpected results.

"hello".slice(3, 1);	// ""

substr(start[, number of characters])Edit

substr extracts characters from the start position, essentially the same as slice.

"hello".substr(1);	// "ello"

When the number of characters is provided, they are extracted by count.

"hello".substr(1, 3);	// "ell"

substring(start[, end])Edit

substring extracts characters from the start position.

"hello".substring(1);	// "ello"

When the end is provided, they are extracted up to, but not including the end position.

"hello".substring(1, 3);	// "el"

substring always works from left to right. If the start position is larger than the end position, substring will swap the values; although sometimes useful, this is not always what you want; different behavior is provided by slice.

"hello".substring(3, 1);	// "el"


This function returns the current string in lower case.

var foo = "Hello!";
alert(foo.toLowerCase());	// hello!


This function returns the current string in upper case.

var foo = "Hello!";
alert(foo.toUpperCase());	// HELLO!

Further readingEdit

Read in another language