JavaScript

JavaScript Indexof Function || JavaScript

JavaScript Indexof

Javascript indexof function is use to find the index of word or letter present in the string. It will give the index of first occurrence of the letter in the string. JavaScript  function can be also use to find out index of element of an JavaScript array. We write element of an array in the parameter of  function.

Example #1:

<!DOCTYPE html>
<html>
<body>
<p id="indexof"></p>
<script>
var string = "Javascript Programming";   
var index = string.indexOf("a");   
document.getElementById("indexof").innerHTML = index;
</script>
<body>
</html>

Output:

1

Other argument is to define the position of letter from which search starts.

Example #2:

<!DOCTYPE html>
<html>
<body>
<p id="indexof"></p>
<script>
var string = "Javascript Programming";   
var index = string.indexOf("a",4);   
document.getElementById("indexof").innerHTML = index;
</script>
<body>
</html>

Output:

16

JavaScript indexof function can be also use to find out index of element of an JavaScript array. We write element of an array in the parameter of indexof function.

Example #3:

<!DOCTYPE html> 
<html> 
<body> 
<script> 

var array = [1,2,3,4,5,6];
var index = array.indexOf(4);
console.log(index);
</script>
</body>
</html>

Output:

3

Example #4:

<!DOCTYPE html> 
<html> 
<body> 
<script> 
var array = ["NODE","REACT","PHP","MYSQL","HTML","CSS"];
var index = array.indexOf("CSS");
console.log(index);
</script>
</body>
</html>

Output:

5

For JavaScript array the paramerter of indexof are: indexof(element , start) . The second parameter indicate the starting index from where searching will start. As following Example:

Example #5:

<!DOCTYPE html> 
<html> 
<body> 
<script> 
var array = ["NODE","CSS","PHP","MYSQL","HTML","CSS"];
var index = array.indexOf("CSS",3);
console.log(index);
</script>
</body>
</html>

Output:

5

Whenever the element in the array or letter in the string or word in the string is not present in that array or string then  function will return value -1.

Example #6:

<!DOCTYPE html> 
<html> 
<body> 
<script> 


var array = ["NODE","REACT","PHP","MYSQL","HTML","CSS"];
var index = array.indexOf("JAVASCRIPT");
console.log(index); 
</script>
</body>
</html>

Output:

-1

Example #7:

<!DOCTYPE html>
<html>
<body>
<p id="indexof"></p>
<script>
var string = "Javascript Programming";   
var index = string.indexOf("z");   
document.getElementById("indexof").innerHTML = index;
</script>
<body>
</html>

Output:

-1

  Note: There are many Compilers you can use to run  the HTML.CSS,JAVASCRIPT and PHP codes like Brackets, visual studio and Atom . The best and recommended editor is Visual Studio if you have a System with Sufficient requirements.

 

For more Examples Related CSS,html and Java Script Visit on Coding Academics

For Web,Java and Assembly language related Semester Projects please Visit on Programming Zone.  

Leave a Reply

Your email address will not be published. Required fields are marked *