Hello Friends đź‘‹,

Welcome To Infinitbility! ❤️

This tutorial will help you to check index exist or not in array, here we will use if…else common syntax to check index present or not in array.

when we pass index in array like this arr[3] it will return value else undefined.

let assume we have namesarray like below.

const names = ["Infinit", "Bility", "Infinitbility", "Welcome"];

when we try 3 and 4 index in this array.

console.log(names[3]);  // Welcome
console.log(names[4]);  // undefined

when we put this index access value syntax in if...else we are able to know index exist or not.

if(names[4]){
  console.log("Exist");
} else {
  console.log("Not Exist");
}

Complete example

const names = ["Infinit", "Bility", "Infinitbility", "Welcome"];
console.log(names[3]);  // Welcome
console.log(names[4]);  // undefined
if(names[4]){
  console.log("Exist");
} else {
  console.log("Not Exist");
}

Output

check index, Javascript, Example