JavaScript

How to Use Not in Operator in JavaScript

The “Not” operator is famous in JavaScript for reversing the logical state of any value. Moreover, it is helpful to validate a specified property in a large list of object properties. It evaluates whether a certain property already exists in the object or not and returns a Boolean output (true or false values). This post focuses on practicing the Not in operator in JavaScript.

How to Use Not in Operator in JavaScript?

In JavaScript, the Not (!) operator is employed to contradict any expression. It is applied with the “in” operator to check the existence of the properties in the object. Additionally, you would have experienced the “Not in” operator, which is utilized to validate the existence of a property and insert it into the object if it is not present. Both methods return Boolean output (true or false values) based on the passing property name. Let us experience the usage of the Not in operator with the help of suitable examples.

Example 1: Using the Not (!) in Operator in JavaScript 

An example is considered to contradict the statement by employing the “Not in” operator in JavaScript.

The operator evaluates the presence of a property in the existing object by returning a Boolean output. For instance, the code is as follows.

Code

console.log("Example to use the Not in Operator");
const teacher_info = {
name: 'Peter',
age:25,
subject: "English",
};
if (!('contact' in teacher_info))
{
teacher_info.contact = "332214353";
}
console.log(teacher_info);

 

The above code is described below:

  • Firstly, a “teacher_info” object is initialized with “name”, “age” and “subject” properties.
  • These properties have “Peter”, “25” and “English” values.
  • After that, the “! in” operator is utilized to evaluate the presence of the “contact” property in the “teacher_info” object.
  • If the “contact” property is not in the “teacher_info” object, then assign a value of “332214353” to the “contact” property.
  • Finally, the log() method is employed to display the “teacher_info” object in the console window.

Output

Earlier, the “contact” property was not present in the object. After applying the “Not in” operator, the if condition is true and therefore the “contact” property is added to the object as shown in the output.

Example 2: Using the Not (!) in Operator in JavaScript 

The Not operator is integrated with the “in” operator to evaluate the existence of a property in the object. The following code aims to validate whether the new property is present or not in the existing object properties.

Code

console.log("Example to use the Not Operator");
const teacher_info = {
name: 'Peter',
age:25,
subject: 'English',
};
if (!('country' in teacher_info))
{
teacher_info.country = "USA";
}
console.log(teacher_info);

 

The code is explained in a list:

  • An object named “teacher_info” is created with different properties.
  • These properties include name, age, and subject, having “Peter”, “25” and “English” values.
  • After that, “Not in” operator is utilized to check the “country” property in the “teacher_info” object.
  • If the “country” property is not present in the “teacher_info” object, then assign the “USA” value.
  • In the end, the log() method is adapted to present the “teacher_info” object in the console window.

Output

The expected outcome returns the property with the value “country: ‘USA'”, which is not present in the “teacher_info” object. It is possible through “Not in” in JavaScript.

Conclusion

In JavaScript, the “Not in” operator is utilized to validate the existence of a specific property by passing an argument. It is useful to check if the new property exists or not in the existing object properties. The method can be applied with the “Not” operator to evaluate the specified property in the current properties of the object. This post enlightens a brief explanation of the “Not in” operator along with two practical examples.

About the author

Syed Minhal Abbas

I hold a master's degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.