String methods help you to work with strings.
The indexOf() method returns the index of (the position of) the first occurrence of a specified text in a string:
The lastIndexOf() method returns the index of the last occurrence of a specified text in a string:
Both the indexOf(), and the lastIndexOf() methods return -1 if the text is not found.
![]() |
JavaScript counts positions from zero. 0 is the first position in a string, 1 is the second, 2 is the third ... |
---|
Both methods accept a second parameter as the starting position for the search.
The search() method searches a string for a specified value and returns the position of the match:
![]() |
Did You Notice? |
---|
The two methods, indexOf() and search(), are equal.
They accept the same arguments (parameters), and they return the same value.
The two methods are equal, but the search() method can take much more powerful search values.
You will learn more about powerful search values in the chapter about regular expressions.
There are 3 methods for extracting a part of a string:
slice() extracts a part of a string and returns the extracted part in a new string.
The method takes 2 parameters: the starting index (position), and the ending index (position).
This example slices out a portion of a string from position 7 to position 13:
The result of res will be:
If a parameter is negative, the position is counted from the end of the string.
This example slices out a portion of a string from position -12 to position -6:
The result of res will be:
If you omit the second parameter, the method will slice out the rest of the string:
or, counting from the end:
![]() |
Negative positions does not work in Internet Explorer 8 and earlier. |
---|
substring() is similar to slice().
The difference is that substring() cannot accept negative indexes.
The result of res will be:
If you omit the second parameter, substring() will slice out the rest of the string.
substr() is similar to slice().
The difference is that the second parameter specifies the length of the extracted part.
The result of res will be:
If the first parameter is negative, the position counts from the end of the string.
The second parameter can not be negative, because it defines the length.
If you omit the second parameter, substr() will slice out the rest of the string.
The replace() method replaces a specified value with another value in a string:
The replace() method can also take a regular expression as the search value.
A string is converted to upper case with toUpperCase():
A string is converted to lower case with toLowerCase():
concat() joins two or more strings:
The concat() method can be used instead of the plus operator. These two lines do the same:
![]() |
All string methods return a new string. They don't modify the original string. Formally said: Strings are immutable: Strings cannot be changed, only replaced. |
---|
There are 2 safe methods for extracting string characters:
The charAt() method returns the character at a specified index (position) in a string:
The charCodeAt() method returns the unicode of the character at a specified index in a string:
You might have seen code like this, accessing a string as an array:
This is unsafe and unpredictable:
If you want to read a string as an array, convert it to an array first.
A string can be converted to an array with the split() method:
If the separator is omitted, the returned array will contain the whole string in index [0].
If the separator is "", the returned array will be an array of single characters:
For a complete reference, go to our Complete JavaScript String Reference.
The reference contains descriptions and examples of all string properties and methods.