How to use indexOf in JavaScript

2 Answers

0 votes
const s = "javascript";

let i = s.indexOf("j");
console.log(i);

i = s.indexOf("a");
console.log(i);

i = s.indexOf("z");
console.log(i);




/*

run:

0
1
-1

*/

 



answered Oct 11, 2020 by avibootz
0 votes
const s = "javascript c++ php javascript" 

let i = s.indexOf("javascript");
console.log(i);

i = s.indexOf("javascript", 5); // Find the word beginning at index 5
console.log(i);



/*

run:

0
19

*/

 



answered Oct 11, 2020 by avibootz

Related questions

...