Learn the basics of the JavaScript in Operator.

The in operator is pretty useful. It allows us to check if an object has a property.

The in operator returns true if the specified property is in the specified object or its prototype chain, this means, that the first operand is a property of the object passed on the right, or a property of one of its ancestors in its prototype chain. Otherwise it returns false.

The specified property could be a string or a symbol representing a property name or array index, take in consideration that the non-symbols will be coerced to string

Example in Object:

class Car {
  constructor() {
    this.wheels = 4;
  }
}

class Accord extends Car {
  constructor(year) {
    super()
    this.brand = 'Honda';
    this.year = year;
  }
}

const myCar = new Accord(2019);

console.log('brand' in myCar);
// expected output: true

console.log('wheels' in myCar);
// expected output: true

console.log('year' in Car);
// expected output: false

Example in Array:

You must specify the index number, not the value at that index

const fruits = ['Apple', 'Orange', 'Banana'];

console.log(0 in fruits);
// expected output: true

console.log(4 in fruits);
// expected output: false

console.log('Apple' in fruits);
// expected output: false

console.log('length' in fruits);
// expected output: true
// length is an Array property

delete fruits[1];
console.log(fruits);
// expected output: ["Apple", empty, "Banana"]

console.log(1 in fruits);
// expected output: false