xFactorSchool.com(Beta)
How to Do Examples
Python Examples
HTML Examples
Code Examples
C Code Examples
C++ Code Examples
C# Code Examples
Java Code Examples
Python Code Examples
HTML Code Examples
CSS Code Examples
JavaScript Code Examples
Online Editor
C Code Editor
C++ Code Editor
C# Code Editor
Java Code Editor
Python Code Editor
HTML Code Editor
CSS Code Editor
JavaScript Code Editor
Practice how to return index of the first occurrence of a specified text in a string in JavaScript
JavaScript Online Editor
Run Code
More JavaScript Sample Codes
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Return Index of the first occurrence of a specified text in a string in JavaScript</title> <!--Script tag is added to write JavaScript code inside it--> <script> let myString = "JavaScript is a fun programming."; let index = myString.indexOf("fun"); //indexOf() method is is used function myFunction() { document.getElementById("outPut").innerHTML = `The index of fun in the string is ${index}.`; } </script> </head> <body> <h1>How to Return Index of the First Occurrence of a Specified Text in a String in JavaScript?</h1> <button onclick="myFunction()">Click here to see result</button> <p id="outPut"></p> </body> </html>