The JavaScript String Object

The string data type is a primitive data type that represents a sequence of characters.

JavaScript provides both a string primitive and a global String object. The String object essentially wraps the primitive string with methods and properties used to access and manipulate individual characters or the entire string.

String Syntax

String objects can be created by calling the new String() constructor or the String() global function:

var string1 = new String([string]);
var string1 = String([string]);

String primitives can be created using the string literal form:

var string2 = 'string';
var string3 = "string";

Conversion between String objects and string primitives is done automatically, thus the methods of the String object can be called on string primitives as well.

The valueOf method of the String object can be used to manually convert a String object to a string primitive.

Accessing individual string characters

Individual characters in a string can be accessed by calling the charAt method with the zero-based index of the desired character:

var name = "Jane Doe";
name.charAt(1); // "a"
name.charAt(4); // " "

It's also possible to use the square brackets notation:

name[0]; // "J"

This feature, however, is not supported in all browsers, so it's best to stick with the charAt method.

String Properties

constructor
Returns a reference to the function that created the String object
length
Returns the number of characters in a string
prototype
Allows the addition of properties and methods to all objects

String Methods

charAt(index)
Returns the character at the specified index
charCodeAt(index)
Returns the Unicode value of the character at the specified index
concat(string1 [, string2 [, ...]])
Creates a new string by joining two or more strings
fromCharCode(num1 [, num2 [, ...]])
Returns a string created by converting the specified Unicode values to characters
indexOf(searchString [, offset])
Returns the position of the first occurrence of a string
lastIndexOf(searchString [, offset])
Returns the position of the last occurrence of a string
match(pattern)
Match a regular expression pattern against the input string
replace(pattern, replacement)
Performs a search and replace operation on the input string using a regular expression
search(pattern)
Searches the input string for the given pattern
slice(offset [, length])
Extracts a section of the string and returns a new string containing the extracted characters
split(pattern [, limit])
Splits the input string into an array using a regular expression
substr(offset [, length])
Returns a part of the string beginning at a specified start position, and through the specified number of characters
substring(indexStart , [indexStop])
Returns a part of the string between two specified indices
toLowerCase()
Makes a string lowercase
toString()
Returns a string representation of the String object
toUpperCase()
Makes a string uppercase
valueOf()
Returns the primitive value of the string

See also

If you see a typo, want to make a suggestion or have anything in particular you'd like to know more about, please drop us an e-mail at hello at diveintojavascript dot com.

Copyright © 2010-2013 Dive Into JavaScript