How to use indexOf() to get the index within a String of the first occurrence of the specified value in JavaScript

3 Answers

0 votes
// str.indexOf(searchValue[, fromIndex])

var s = 'Online JavaScript Cloud Products';

document.write(s.indexOf('JavaScript') + "<br />"); // 7
document.write('PHP JavaScript'.indexOf('PHP') + "<br />"); // 0
document.write(s.indexOf('Online') + "<br />"); // 0
document.write(s.indexOf('Onlinee') + "<br />"); // -1
document.write(s.indexOf('JavaScript', 0) + "<br />"); // 7
document.write(s.indexOf('JavaScript', 7) + "<br />"); // 7
document.write(s.indexOf('', 1) + "<br />"); // 1
document.write(s.indexOf('', 7) + "<br />"); // 7
document.write(s.indexOf('', 130) + "<br />"); // 32
document.write(s.indexOf('', 280) + "<br />"); // 32


/*
run:

7
0
0
-1
7
7
1
7
32
32

*/

 



answered Aug 10, 2016 by avibootz
0 votes
// str.indexOf(searchValue[, fromIndex])

var s = 'Online JavaScript Cloud Products';

document.write(s.indexOf('JavaScript') + "<br />"); 
document.write(s.indexOf('JAVASCRIPT') + "<br />"); 



/*
run:

7
-1

*/

 



answered Aug 10, 2016 by avibootz
edited Aug 10, 2016 by avibootz
0 votes
// str.indexOf(searchValue[, fromIndex])

var s = 'Online JavaScript Cloud Products';

document.write(s.indexOf('JavaScript') + "<br />"); 
document.write(s.indexOf('JavaScrapt') + "<br />"); 



/*
run:

7
-1

*/

 



answered Aug 10, 2016 by avibootz
...