The JavaScript String indexOf() method is a powerful tool that allows you to search for the first occurrence of a specified substring within a given string. It returns the index position of the substring if found, or -1 if the substring is not present, which makes it useful for various string manipulation tasks.
In this article will discuss the basics like its definition, syntax, examples, and use cases.
What is indexOf () in JavaScript?
The indexOf() method is a built-in JavaScript function that returns the first index at which a given element can be found in an array or string, or -1 if it is not present. In other words, it helps to find the position of a particular element in a string or array.
Syntax of indexOf()
Let's check out the basic syntax of the indexOf() method:
array.indexOf(element, start)
element: The element to locate in the array.
start (optional): The index to start the search from. If not specified, it starts from index 0.
Using indexOf() with Strings
Here's an example of using indexOf() with strings:
let str = "Hello, world!";
let position = str.indexOf("world");
console.log(position);
You can also try this code with Online Javascript Compiler
Here, indexOf() returns 2, which is the index of "Mango" in the array arr.
Case Sensitivity and Return Values
Remember that indexOf() is case-sensitive. It treats "a" and "A" as different characters. If indexOf() doesn't find the element, it returns -1.
JavaScript String indexOf() method examples
The indexOf() method in JavaScript is used to find the first occurrence of a substring within a given string. It returns the index of the first character of the first occurrence of the specified substring, or -1 if the substring is not found. Here are some examples of how to use the indexOf() method:
Basic Usage
Javascript
Javascript
const text = "Hello, world!"; const position = text.indexOf("world"); console.log(position);
You can also try this code with Online Javascript Compiler
const text = "This is a test. Is this test good?"; let position = text.indexOf("is"); while (position !== -1) { console.log(`Found "is" at index ${position}`); position = text.indexOf("is", position + 1); }
You can also try this code with Online Javascript Compiler
The indexOf() method is a useful tool for searching and manipulating strings in JavaScript. It's worth noting that there is also a related method called lastIndexOf(), which finds the last occurrence of a substring in a string.
Difference Between String indexOf() and String search()
Aspect
String indexOf()
String search()
Return Value
Returns the index of the first occurrence of the
Returns the index of the first occurrence of the
specified substring, or -1 if not found.
specified substring, or -1 if not found.
Syntax
str.indexOf(searchValue, fromIndex)
str.search(regexp)
Parameter Type
Accepts a string as the search value.
Accepts a regular expression as the search pattern.
Case-sensitivity
Case-sensitive by default.
Case-sensitive by default, but can be modified using flags.
Start Position
Optional second parameter specifies the start
Always searches from the beginning of the string.
position for the search.
Regular Expressions
Does not support regular expressions as the search
Supports regular expressions as the search pattern,
value.
allowing for more advanced search capabilities.
Browser Compatibility
Supported in all major browsers.
Supported in all major browsers.
Frequently Asked Questions
What is the use of indexOf in JavaScript?
In JavaScript, the indexOf method is used to determine the first index at which a given element can be found in an array or a substring in a string, returning -1 if not found.
What is the index in JavaScript?
In JavaScript, the index is the position of an element in an array or a character in a string, starting from 0.
What is the difference between indexOf and contains in JavaScript?
indexOf is a method that returns the index of the first occurrence of a value in a string or array, while includes (not "contains") returns a boolean indicating if a value is present.
Conclusion
In this article, we talked about the JavaScript String indexOf() method, which searches for a specified substring within a string and returns its index position. It is a simple but effective way to locate substrings and perform various string manipulation tasks in JavaScript.We also saw the difference between String indexOf() and String search().
To learn more about web development, we recommend reading the following articles: