loading...

πŸ”₯ Quick Tip: Understanding the difference between operators "in" and "hasOwnProperty"

helderburato profile image Helder Burato Berto ・Updated on ・1 min read

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

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! ✨

Discussion

pic
Editor guide