JavaScript search()
Syntax
The syntax for the search()
method in JavaScript is:
string.search(searchvalue)
where string
is the string to be searched, and searchvalue
is the value to search for. The search()
method returns the index of the first occurrence of the search value in the given string, and -1 if the search value is not found.
Example
let str = "Hello World!";
let pos = str.search("World");
console.log(pos); // Output: 6
In this example, the search()
method is used to search for the string "World" in the str
variable. The method returns the index of the first occurrence of "World" in str
, which is 6.
Output
The output of the search()
method is the index of the first occurrence of the search value in the given string, and -1 if the search value is not found.