Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

JavaScript IndexOf

by suresh

The JavaScript indexof method returns the index position of the first occurrence of a specified string. If the specified string not located, the indexof function will return -1. The syntax of the JavaScript indexof function is

String_Object.indexof(Substring, Starting_Position)
  • String_Object: Valid String Object or literal.
  • Substring: String you want to search inside the string_Object.
  • Starting_Position: This is an optional parameter. If you want to specify the starting point (starting index position), then please specify the index value here.

NOTE:

  • If the Starting_Position is a Negative number, JavaScript IndexOf starts looking from Zero.
  • If the Starting_Position is Out of range Index, IndexOf function start looking from the highest index number.

JavaScript indexof Example

The following set of examples will help you understand the indexof Function.

<!DOCTYPE html>
<html>
<head>
    <title> JavaScript IndexOf </title>
</head>
<body>
    <h1> JavaScript IndexOf </h1>
<script>
 var Str1 = "Learn JavaScript at Tutorial Gateway.org";
 var Str2 = "We are abc working at abc company";
 var Str3 = Str1.indexOf("Script");
 var Str4 = Str1.indexOf("abc"); // Non existing item
 var Str5 = Str2.indexOf("abc");
 var Str6 = Str1.indexOf("Script", 5);
 var Str7 = Str2.indexOf("abc", 10);
 
 document.write("<b> Index position of Script is:</b> " + Str3);
 document.write("<br \> <b> Index position of abc is:</b> " + Str4);
 document.write("<br \> <b> Index position of abc is:</b> " + Str5);
 document.write("<br \> <b> Index position of Script is:</b> " + Str6);
 document.write("<br \> <b> Index position of abc is:</b> " + Str7);
</script>
</body>
</html>

TIP: The index position in JavaScript indexof Function will start from 0, Not 1.

JavaScript IndexOf

The following statement finds the index position of a substring ‘Script’ and stores the index value in Str3.

var Str3 = Str1.indexOf("Script");

In the next line, we are looking for a non-existing “abc” inside the Str1. Since JS indexof function doesn’t find the substring, it is returning -1 as output

 var Str4 = Str1.indexOf("abc"); // Non existing item

Here, we are looking for “abc” inside the Str2 using IndexOf function.

var Str5 = Str2.indexOf("abc");

From the above statement, though abc reappeared multiple times, the Javascript indexof function written the index position of a first occurrence. Now, let us modify the starting position from default 0 to 10

 var Str7 = Str2.indexOf("abc", 10);

The above code returns the first occurrence of string abc starting at index position 10.

Placed Under: JavaScript

  • SQL DML, DDL, DCL & TCL Cmds
  • SQL NOT EXISTS Operator
  • SQL UPDATE from SELECT
  • SQL AFTER UPDATE Triggers
  • SQL Get Column Names in Table
  • SQL IF ELSE
  • SQL ACID Properties
  • SQL FOR XML PATH
  • Java Two Dimensional Array
  • Java Perfect Number Program
  • Java Count Digits in a Number
  • C Compare Two Strings Program
  • C Print Prime Numbers 1 to 100
  • C program to Reverse a String
  • C Palindrome Number Program
  • C Program for Palindrome String
  • C Remove Duplicate String Chars
  • C Square of a Number Program
  • C Sum and Average of N Number
  • Python Fibonacci Series program
  • Python Area Of Circle Program
  • Python Prime Numbers 1 to 100
  • Python Program for Leap Year
  • Tableau Rank Calculation
  • Tableau Google Maps usage
  • Power BI Format Dates
  • Power BI Top 10 Filters
  • Power BI – Create Hierarchy
  • Power BI DAX Math Functions
  • Learn SSIS in 28 Days
  • SSIS Transformations
  • SSIS Incremental Load
  • SSRS Drill Through Reports
  • SSRS Drill Down Reports
  • R Programming Tutorial
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy