The ‘in’ operator can help to check if an Array contains specified index or an Object contains a specified key.
Checking if index exists in an Array:
1 2 3 4 |
const programmingLanguages = ['Ada', 'Basic', 'Cobol', 'Dart']; console.log(0 in programmingLanguages); // => true console.log(3 in programmingLanguages); // => true console.log(4 in programmingLanguages); // => false |
Checking if an Object contains a property:
1 2 3 4 |
const person = {firstName : 'Roman', lastName: 'L.'}; console.log('firstName' in person); // => true console.log('lastName' in person); // => true console.log('age' in person); // => false |