DEV Community 👩‍💻👨‍💻

Helder B. Berto
Helder B. 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)

🌚 Friends don't let friends browse without dark mode.

Good news! You can update to dark mode in your DEV settings.