What is indexOf in javascript?

 Array indexOf method javascript:

 

indexOf() method find position index that element available. if we will define the Second parameter then it's the return start position. And if we will not find the given value then it will return -1.

Syntax of indexOf method:

array.indexOf()

 

indexOf example in javascript:

 

const colors = ["Red", "blue", "Black", "Red", "Orange", "White"];

 

console.log(colors.indexOf('Red'));

// 0

 

console.log(colors.indexOf('Red', 1));

// start from index 1

// expected output: 3

 

console.log(colors.indexOf('Green'));

// expected output: -1



No comments:

Note: Only a member of this blog may post a comment.

Copyright Reserved to Anything Learn. Powered by Blogger.