What means exclamation mark before !obj.hasOwnProperty(srcKeys[i])

What means exclamation mark before !obj.hasOwnProperty(srcKeys[i])

function whatIsInAName(collection, source) {

  var srcKeys = Object.keys(source);

  // filter the collection
  return collection.filter(function(obj) {
    for (var i = 0; i < srcKeys.length; i++) {
      if (
        !obj.hasOwnProperty(srcKeys[i]) ||
        obj[srcKeys[i]] !== source[srcKeys[i]]
      ) {
        return false;
      }
    }
    return true;
  });
}

// test here
whatIsInAName(
  [
    { first: "Romeo", last: "Montague" },
    { first: "Mercutio", last: null },
    { first: "Tybalt", last: "Capulet" }
  ],
  { last: "Capulet" }
);

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

in plain English it means “is not true”.

const myObject={
  someProp: ''
}

console.log(myObject.hasOwnProperty('someProp'))  //true

delete myObject.someProp

console.log(myObject.hasOwnProperty('someProp'))  //false
console.log(!myObject.hasOwnProperty('someProp'))  //true
2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.