Categories
JavaScript Answers

How to check if an array item is set in JavaScript?

Spread the love

To check if an array item is set in JavaScript, we use the in operator.

For instance, we write

const assocPagine = {
  home: 0,
  about: 1,
  work: 2,
};

if ("work" in assocPagine) {
  // ...
}

to check if the work property is in the assocPagine object as a property in the object itself or as an inherited property.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *