Account
Categories

JavaScript indexOf() Method


Definition:

When you use this function in a program, it searches for a specific character or word in a string and returns its position (index number).

If the value is not found, it returns -1.

If the value is found, it returns the index of the first occurrence (starting from 0).

Syntax:

stringVariable.indexOf(searchValue)

Explanation:

Whereas,
string → A type of data that stores text.
variable → Used to store a value.
indexOf(searchValue) → Used to find the position of a character or word in a string. Returns -1 if not found

Example:

let name = "edutation.com";
console.log(name.indexOf("d")); // 1
console.log(name.indexOf("x")); // -1

Output: