# findindex method in javascript

<div class="WaaZC" id="bkmrk-the%C2%A0findindex%28%29%C2%A0meth" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="rPeykc" data-hveid="CBwQAQ" data-ved="2ahUKEwjh78_JjaaRAxUszjgGHWHvCPgQo_EKegQIHBAB" style="margin: 0px 0px 10px;"><span data-huuid="11840898147529364018">The `findIndex()` method in JavaScript is an `Array` method that returns the index of the first element in an array that satisfies a provided testing function. </span><span data-huuid="11840898147529364321">If no element in the array satisfies the condition, it returns -1.<span class="pjBG2e" data-cid="7c7e8e0e-d575-44f5-a751-8eac3b50769b"><span class="UV3uM" style="white-space: nowrap;"> </span></span></span><div class="NPrrbc" data-cid="7c7e8e0e-d575-44f5-a751-8eac3b50769b" data-uuids="11840898147529364018,11840898147529364321" style="margin-inline-end: 6px; vertical-align: middle; display: inline-flex;"><div aria-label="View related links" class="BMebGe btku5b LwdV0e FR7ZSc OJeuxf" data-hveid="CCAQAQ" data-ved="2ahUKEwjh78_JjaaRAxUszjgGHWHvCPgQ3fYKegQIIBAB" jsaction="KjsqPd" jsname="HtgYJd" role="button" style="display: inline-flex !important; vertical-align: middle; cursor: pointer; outline: 0px; -webkit-tap-highlight-color: transparent; color: rgb(28, 65, 154);" tabindex="0"><div class="niO4u" style="display: flex; justify-content: center; position: relative; align-items: center; width: 28px; z-index: 0; box-sizing: border-box; border-radius: 9999px; padding: 0px; border: 1px solid transparent; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(229, 237, 255) !important; height: 20px; margin: 0px auto; min-height: 20px;"><span class="TwMgNb Hkv2Pe" style="color: rgb(31, 31, 31) !important; background: unset !important; display: flex; align-items: center;"><span class="iPjmzb Sorfoc gNGSDf" style="display: flex; height: unset; rotate: 135deg;"><span class="z1asCe Sb7k4e" style="display: inline-block; fill: currentcolor; height: 18px; line-height: 18px; position: relative; width: 18px;"><svg aria-hidden="true" focusable="false" viewbox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M3.9 12c0-1.71 1.39-3.1 3.1-3.1h4V7H7c-2.76 0-5 2.24-5 5s2.24 5 5 5h4v-1.9H7c-1.71 0-3.1-1.39-3.1-3.1zM8 13h8v-2H8v2zm9-6h-4v1.9h4c1.71 0 3.1 1.39 3.1 3.1s-1.39 3.1-3.1 3.1h-4V17h4c2.76 0 5-2.24 5-5s-2.24-5-5-5z"></path></svg></span></span></span></div></div></div></div></div><div class="WaaZC" id="bkmrk-syntax%3A" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CBkQAQ" data-ved="2ahUKEwjh78_JjaaRAxUszjgGHWHvCPgQo_EKegQIGRAB" role="heading" style="margin: 20px 0px 10px; font-size: 20px; font-weight: 600; line-height: 28px;"><span data-huuid="11840898147529364927">Syntax:</span></div></div><div class="WaaZC" id="bkmrk-javascript" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div jsaction="rcuQ6b:npT2md" jscontroller="JegcYe"><div class="ecCNFc" style="border-image: none 100% / 1 / 0 stretch; border-radius: 8px; font-size: 14px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5); margin-bottom: 20px; color: rgb(10, 10, 10); border: 0.8px solid rgb(169, 172, 170);"><div class="zYSUYd" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 8px 8px 0px 0px;"><div class="FS7GEb" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 518.4px;"><div class="dDrxod" style="height: 34px; display: flex; justify-content: space-between; align-items: center; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(229, 237, 255); border-bottom: 0.8px solid rgb(169, 172, 170);"><div class="x7ndcb" style="padding-left: 16px; display: flex; align-items: center; color: rgb(10, 10, 10);">JavaScript</div><div aria-live="polite" class="WDoJJe" style="margin-right: 4px; display: flex; align-items: center;"><button aria-label="Copy code to clipboard" class="hqI3tf B4zsNc" data-ved="2ahUKEwjh78_JjaaRAxUszjgGHWHvCPgQ9MILegQIGxAB" role="button" style="margin: 0px 2px; background: none; height: 34px; border: none; color: rgb(5, 40, 97); position: relative; top: 2px; outline: 0px;" tabindex="0" title="Copy"><span class="z1asCe wm4nBd" style="display: inline-block; fill: currentcolor; height: 20px; line-height: 20px; position: relative; width: 20px;"><svg aria-hidden="true" enable-background="new 0 0 24 24" focusable="false" height="24" viewbox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><g><rect fill="none" height="24" width="24"></rect></g><g><path d="M16,20H5V6H3v14c0,1.1,0.9,2,2,2h11V20z M20,16V4c0-1.1-0.9-2-2-2H9C7.9,2,7,2.9,7,4v12c0,1.1,0.9,2,2,2h9 C19.1,18,20,17.1,20,16z M18,16H9V4h9V16z"></path></g></svg></span></button></div></div></div></div><div class="QQjpRc" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 0px 0px 8px 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5);"><div class="FS7GEb" jsname="zEXxAd" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 518.4px;"></div></div></div></div></div>```
arr.findIndex(callback(element, index, array), thisArg)
```

<div class="WaaZC" id="bkmrk-parameters%3A" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CBcQAQ" data-ved="2ahUKEwjh78_JjaaRAxUszjgGHWHvCPgQo_EKegQIFxAB" role="heading" style="margin: 20px 0px 10px; font-size: 20px; font-weight: 600; line-height: 28px;"><span data-huuid="11840898147529366139">Parameters:</span></div></div><div class="WaaZC" id="bkmrk-callback%3A%C2%A0a-function" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">- <span data-huuid="11840898147529367048">`callback`: </span><span data-huuid="11840898147529367351">A function that is executed for each element in the array. </span><span data-huuid="11840898147529363558">It takes up to three arguments:</span>
    - <span data-huuid="11840898147529364467">`element`: </span><span data-huuid="11840898147529364770">The current element being processed in the array.</span>
    - <span data-huuid="11840898147529365376">`index` (optional): </span><span data-huuid="11840898147529365679">The index of the current element being processed.</span>
    
    <div class="bsmXxe" role="none" style="display: contents;">  
    </div>
- <span data-huuid="11840898147529366285">`array` (optional): </span><span data-huuid="11840898147529366588">The array `findIndex()` was called upon.</span>
- <div class="bsmXxe" id="bkmrk-" role="none" style="display: contents;"></div>

<div class="bsmXxe" id="bkmrk-thisarg%C2%A0%28optional%29%3A%C2%A0" role="none" style="display: contents;"><span data-huuid="11840898147529367194">`thisArg` (optional): </span><span data-huuid="11840898147529367497">A value to be used as `this` when executing the `callback` function.</span></div></div><div class="WaaZC" id="bkmrk-return-value%3A" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="bsmXxe" id="bkmrk-return-value%3A-1" role="none" style="display: contents;"><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CHsQAQ" data-ved="2ahUKEwjh78_JjaaRAxUszjgGHWHvCPgQo_EKegQIexAB" role="heading" style="margin: 20px 0px 10px; font-size: 20px; font-weight: 600; line-height: 28px;"><span data-huuid="17286977328008571702">Return Value:</span></div></div></div><div class="WaaZC" id="bkmrk-the-index-of-the-fir" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="bsmXxe" id="bkmrk-the-index-of-the-fir-1" role="none" style="display: contents;"><div class="bsmXxe" id="bkmrk-the-index-of-the-fir-2" role="none" style="display: contents;"><span data-huuid="17286977328008571121">The index of the first element in the array that satisfies the provided test function.</span></div><div class="bsmXxe" id="bkmrk--1-if-no-element-sat" role="none" style="display: contents;"><span data-huuid="17286977328008572099">-1 if no element satisfies the condition.<span class="pjBG2e" data-cid="28f9d29e-8d19-48af-a126-8d1d2d128136"><span class="UV3uM" style="white-space: nowrap;"> </span></span></span><div class="NPrrbc" data-cid="28f9d29e-8d19-48af-a126-8d1d2d128136" data-uuids="17286977328008571121,17286977328008572099" style="margin-inline-end: 6px; vertical-align: middle; display: inline-flex;"><div aria-label="View related links" class="BMebGe btku5b LwdV0e FR7ZSc OJeuxf" data-hveid="CLABEAE" data-ved="2ahUKEwjh78_JjaaRAxUszjgGHWHvCPgQ3fYKegUIsAEQAQ" jsaction="KjsqPd" jsname="HtgYJd" role="button" style="display: inline-flex !important; vertical-align: middle; cursor: pointer; outline: 0px; -webkit-tap-highlight-color: transparent; color: rgb(28, 65, 154);" tabindex="0"><div class="niO4u" style="display: flex; justify-content: center; position: relative; align-items: center; width: 28px; z-index: 0; box-sizing: border-box; border-radius: 9999px; padding: 0px; border: 1px solid transparent; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(229, 237, 255) !important; height: 20px; margin: 0px auto; min-height: 20px;"><span class="TwMgNb Hkv2Pe" style="color: rgb(31, 31, 31) !important; background: unset !important; display: flex; align-items: center;"><span class="iPjmzb Sorfoc gNGSDf" style="display: flex; height: unset; rotate: 135deg;"><span class="z1asCe Sb7k4e" style="display: inline-block; fill: currentcolor; height: 18px; line-height: 18px; position: relative; width: 18px;"><svg aria-hidden="true" focusable="false" viewbox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M3.9 12c0-1.71 1.39-3.1 3.1-3.1h4V7H7c-2.76 0-5 2.24-5 5s2.24 5 5 5h4v-1.9H7c-1.71 0-3.1-1.39-3.1-3.1zM8 13h8v-2H8v2zm9-6h-4v1.9h4c1.71 0 3.1 1.39 3.1 3.1s-1.39 3.1-3.1 3.1h-4V17h4c2.76 0 5-2.24 5-5s-2.24-5-5-5z"></path></svg></span></span></span></div></div></div></div></div></div><div class="WaaZC" id="bkmrk-example%3A" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="bsmXxe" id="bkmrk-example%3A-1" role="none" style="display: contents;"><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CHUQAQ" data-ved="2ahUKEwjh78_JjaaRAxUszjgGHWHvCPgQo_EKegQIdRAB" role="heading" style="margin: 20px 0px 10px; font-size: 20px; font-weight: 600; line-height: 28px;"><span data-huuid="12270555766286201596">Example:</span></div></div></div><div class="WaaZC" id="bkmrk-javascript-1" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="bsmXxe" role="none" style="display: contents;"><div jsaction="rcuQ6b:npT2md" jscontroller="JegcYe"><div class="ecCNFc" style="border-image: none 100% / 1 / 0 stretch; border-radius: 8px; font-size: 14px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5); margin-bottom: 20px; color: rgb(10, 10, 10); border: 0.8px solid rgb(169, 172, 170);"><div class="zYSUYd" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 8px 8px 0px 0px;"><div class="FS7GEb" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 518.4px;"><div class="dDrxod" style="height: 34px; display: flex; justify-content: space-between; align-items: center; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(229, 237, 255); border-bottom: 0.8px solid rgb(169, 172, 170);"><div class="x7ndcb" style="padding-left: 16px; display: flex; align-items: center; color: rgb(10, 10, 10);">JavaScript</div><div aria-live="polite" class="WDoJJe" style="margin-right: 4px; display: flex; align-items: center;"><button aria-label="Copy code to clipboard" class="hqI3tf B4zsNc" data-ved="2ahUKEwjh78_JjaaRAxUszjgGHWHvCPgQ9MILegQIcxAB" role="button" style="margin: 0px 2px; background: none; height: 34px; border: none; color: rgb(5, 40, 97); position: relative; top: 2px; outline: 0px;" tabindex="0" title="Copy"><span class="z1asCe wm4nBd" style="display: inline-block; fill: currentcolor; height: 20px; line-height: 20px; position: relative; width: 20px;"><svg aria-hidden="true" enable-background="new 0 0 24 24" focusable="false" height="24" viewbox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><g><rect fill="none" height="24" width="24"></rect></g><g><path d="M16,20H5V6H3v14c0,1.1,0.9,2,2,2h11V20z M20,16V4c0-1.1-0.9-2-2-2H9C7.9,2,7,2.9,7,4v12c0,1.1,0.9,2,2,2h9 C19.1,18,20,17.1,20,16z M18,16H9V4h9V16z"></path></g></svg></span></button></div></div></div></div><div class="QQjpRc" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 0px 0px 8px 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5);"><div class="FS7GEb" jsname="zEXxAd" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 518.4px;"></div></div></div></div></div></div>```
const numbers = [2, 8, 1, 3, 4];

// Find the index of the first odd number
const firstOddIndex = numbers.findIndex(function(element) {
  return element % 2 !== 0;
});

console.log(firstOddIndex); // Output: 2 (because 1 is at index 2)

// Find the index of a number greater than 5
const greaterThanFiveIndex = numbers.findIndex(element => element > 5);

console.log(greaterThanFiveIndex); // Output: 1 (because 8 is at index 1)

// Find the index of an element that doesn't exist
const nonExistentIndex = numbers.findIndex(element => element === 10);

console.log(nonExistentIndex); // Output: -1
```