Navigate back to the homepage

How to check if index exists in array javascript?

Infinitbility
JavaScript
November 5th, 2021 · 1 min read
How to check if index exists in array javascript?

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.

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

when we try 3 and 4 index in this array.

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

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

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

Complete example

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

Output

check index, Javascript, Example

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo

Tutorials

Categories

More articles from Infinitbility

How to check value exist in array of object in javascript?

How to check value exist in array of object in javascript?

Example of check value exist in array of object in javascript, React, React Native, vue, node, deno, and all javascript framework.

November 4th, 2021 · 1 min read
How to check value is decimal or not in javascript?

How to check value is decimal or not in javascript?

Example of check value is decimal or not in javascript, React, React Native, vue, node, deno, and all javascript framework.

November 3rd, 2021 · 1 min read
© 2020–2021 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]