Use of these function you can get the position of string or array such as-

Syntax:
array.indexOf(item, start) //For getting position of Array with start number
Syntax: array.indexOf(item) //For getting position of Array
Syntax: str.indexOf(item) //For getting position of string

var arr= [“A”, “B”, “C”, “D”];
arr.indexOf(“C”);

Output will be 2

var arr= [“A”, “B”, “C”, “D”, “E”, “F”, “C”, “G”];
arr.indexOf(“C”, 4);

Output will be 6

var arr= [“A”, “B”, “C”, “D”, “E”, “F”, “C”, “G”];
arr.indexOf(“C”, 1);

Output will be 2

Here, I’m going describe for string Same function.

var str= ‘I l.ove my india’;
str.indexOf(“m”);

Output will be 7

Example:
var
names = [‘Ravi’, ‘Ajay’, ‘Kamal’, ‘Sonu’, ‘Ajay’];

console.log(names.indexOf(‘Ajay’));

// expected output: 1

// start from index 2

console.log(names.indexOf(‘Ajay’, 2));

// expected output: 4

console.log(names.indexOf(‘Ramu’));

// expected output: -1

Note: The indexOf method was introduced in JavaScript 1.6, and does not work in Internet Explorer 8 and earlier versions.

News Reporter
IT Software and web development, Core PHP, JavaScript, JQuery, HTML, CSS, CakePHP, Angularjs, Codeigniter etc.

Leave a Reply

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

error: