js string does not contain 1

js string does not contain

"this is the string".indexOf("cake"); // -1 (does not contain)
"this string has cake".indexOf("cake"); // 16 (contains)

Here is what the above code is Doing:
1. The first line is creating a variable called “string” and assigning it the value of “this is the string”.
2. The second line is creating a variable called “index” and assigning it the value of the index of the first occurrence of the string “cake” in the string “this is the string”.
3. The third line is creating a variable called “index” and assigning it the value of the index of the first occurrence of the string “cake” in the string “this string has cake”.

The indexOf() method returns the index of the first occurrence of the string you pass to it. If the string you pass to it does not exist in the string you call it on, it returns -1.

Similar Posts