HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
The indexOf() method returns the index of (the position of) the first occurrence of a specified text in a string
The indexOf()
method returns the index of (the position of)
the first
occurrence of a specified text in a string:
let str = "Please locate where 'locate' occurs!";
str.indexOf("locate");
JavaScript counts positions from zero.
0 is the first position in a string, 1 is the second, 2 is the third, ...
The lastIndexOf()
method returns the index of the last
occurrence of a specified text in a string:
let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("locate");
Both indexOf()
, and lastIndexOf()
return -1
if the text is not found:
let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("John");
Both methods accept a second parameter as the starting position for the search:
let str = "Please locate where 'locate' occurs!";
str.indexOf("locate", 15);
The lastIndexOf()
methods searches backwards
(from the end to the beginning), meaning:
if the second parameter is 15
, the search starts at position
15, and searches to the beginning of the string.
let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("locate", 15);
The search()
method searches a string for a specified value
and returns the position of the match:
let str = "Please locate where 'locate' occurs!";
str.search("locate");
The two methods, indexOf()
and search()
, are equal?
They accept the same arguments (parameters), and return the same value?
The two methods are NOT equal. These are the differences:
search()
method cannot take a second start position argument.indexOf()
method cannot take
powerful search values (regular expressions).You will learn more about regular expressions in a later chapter.
The match() method searches a string for a match against a regular expression, and returns the matches, as an Array object.
Search a string for "ain":
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/g);
Read more about regular expressions in the chapter JS RegExp.
If a regular expression does not include the g modifier (to perform a global search), the match() method will return only the first match in the string.
regexp | Required. The value to search for, as a regular expression. |
Returns: | An Array, containing the matches, one item for each match, or null if no match is found |
Perform a global, case-insensitive search for "ain":
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/gi);
The includes()
method returns true if a string contains a specified value.
let text = "Hello world, welcome to the universe.";
text.includes("world");
searchvalue | Required. The string to search for |
start | Optional. Default 0. Position to start the search |
Returns: | Returns true if the string contains the value, otherwise false |
JS Version: | ES6 (2015) |
Check if a string includes "world", starting the search at position 12:
let text = "Hello world, welcome to the universe.";
text.includes("world", 12);
includes()
is an ES6 feature (JavaScript 2015).
It is supported in all modern browsers:
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
includes()
is not supported in Internet Explorer.
The startsWith()
method returns true
if a string begins with a specified value, otherwise false
:
let text = "Hello world, welcome to the universe.";
text.startsWith("Hello");
Parameter | Description |
---|---|
searchvalue | Required. The value to search for. |
start | Optional. Default 0. The position to start the search. |
let text = "Hello world, welcome to the universe.";
text.startsWith("world") // Returns false
let text = "Hello world, welcome to the universe.";
text.startsWith("world", 5) // Returns false
let text = "Hello world, welcome to the universe.";
text.startsWith("world", 6) // Returns true
The startsWith()
method is case sensitive.
startsWith()
is an ES6 feature (JavaScript 2015).
It is supported in all modern browsers:
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
startsWith()
is not supported in Internet Explorer.
The endsWith()
method returns true
if a string ends with a specified value, otherwise false
:
Check if a string ends with "Doe":
let text = "John Doe";
text.endsWith("Doe");
Parameter | Description |
---|---|
searchvalue | Required. The value to search for. |
length | Optional. The length to search. |
Check if the 11 first characters of a string ends with "world":
let text = "Hello world, welcome to the universe.";
text.endsWith("world", 11);
The endsWith()
method is case sensitive.
endsWith()
is an ES6 feature (JavaScript 2015).
It is supported in all modern browsers:
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
endsWith()
is not supported in Internet Explorer.
For a complete String reference, go to our:
Complete JavaScript String Reference.
The reference contains descriptions and examples of all string properties and methods.