[Javascript] Javascript 'in' opreator

If you want to check whether a key is inside an Object or Array, you can use 'in':

Object:

const obj = {
  name: 'GraphQL',
  watched: false
};

console.log('name' in obj); // true
console.log('title' in obj); // false

Array:

const characters = [
  'Harry Potter',
  'Ron Weasly',
  'Hermione Granger',
  'Nevel Longbottom',
  'Lavender Brown',
  'Scabbers',
  'Pigwidgeon',
]
console.log(0 in characters); // true;
console.log(6 in characters); // true;
原文地址:https://www.cnblogs.com/Answer1215/p/6275303.html