DEV Community

Helder Burato Berto
Helder Burato Berto

Posted on • Updated on

🔥 Quick Tip: Understanding the difference between operators "in" and "hasOwnProperty"

On this simple trick, I'll show the difference between operators in and hasOwnProperty.

class Validator {
  static isValid() {
    return true;
  }
}

class EmailValidator extends Validator {
  static checkEmail(email) {
    return true;
  }
}

console.log('isValid' in EmailValidator); // => true
console.log(EmailValidator.hasOwnProperty('isValid')); // => false
Enter fullscreen mode Exit fullscreen mode

The operator "in":

It enables us to check inheritance keys like in the example above.

The operator "hasOwnProperty":

It only returns true if the object that property directly and not from parents.


Did you like it? Comment, share! ✨

Top comments (0)