Ads Top

What is indexOf in javascript?

 Array indexOf method javascript:

 

indexOf() method find position index that element available. if we will define Second parameter then it's start position. And if we will not find given value then it will be 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.