The Wayback Machine - https://web.archive.org/web/20241118204002/https://www.geeksforgeeks.org/javascript-in-operator/
Open In App

JavaScript in Operator

Last Updated : 13 Dec, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

JavaScript in operator is an inbuilt operator which is used to check whether a particular property exists in an object or not. It returns a boolean value true if the specified property is in an object, otherwise, it returns false.

Syntax:

prop in object

Parameters:

  • prop: This parameter holds the string or symbol that represents a property name or array index.
  • object: This parameter is an Object that is to be checked whether it contains the prop.

Return value:

This method returns a boolean value. The value true is returned if the specified property is found in an object, else it returns false.

Example 1: Below is an example of the in-operator.

Javascript




function gfg() {
    // Illustration of in operator
    const array = ['geeks', 'for',
        'geeks']
 
    // Output of the indexed number
    console.log(0 in array);
 
    // Output of the Value
    console.log('for' in array);
 
    // output of the Array property
    console.log('length' in array);
}
gfg();


Output

true
false
true

Example 2: This example shows the use of the in operator in Javascript.

Javascript




// Illustration of in operator
const array = ['geeksforgeeks', 'geeksfor',
    'geeks', 'geeks1']
 
// Output of the indexed number
console.log(0 in array)
console.log(2 in array)
console.log(5 in array)
 
// Output of the Value
console.log('for' in array)
console.log('geeksforgeeks' in array)
 
// output of the Array property
console.log('length' in array)


Output

true
true
false
false
false
true

Example 3: This example shows the use of the in operator in Javascript.

Javascript




// Illustration of in operator
const object = {
    val1: 'Geeksforgeeks',
    val2: 'Javascript',
    val3: 'operator',
    val4: 'in'
};
 
console.log('val1' in object);
 
delete object.val1;
console.log('val1' in object);
 
if ('val1' in object === false) {
    object.val1 = 'GEEKSFORGEEKS';
}
 
console.log(object.val1);


Output

true
false
GEEKSFORGEEKS

Supported Browsers:

  • Google Chrome
  • Firefox
  • Opera
  • Safari
  • Edge
  • Internet Explorer

We have a complete list of Javascript Operators, to check those please go through the Javascript Operators Complete Reference article.



Previous Article
Next Article

Similar Reads

Why we should prefer square operator over the dot operator to access the value from the object ?
The square notation should be used as much as possible as compared to the dot notation to access the value from the object in JavaScript. Let's first understand how to obtain the value by using the square notation and dot notation from the object. Square notation: In the square notation the keyName that is passed is treated as a variable and search
3 min read
JavaScript Course Conditional Operator in JavaScript
JavaScript Conditional Operators allow us to perform different types of actions according to different conditions. We make use of the 'if' statement. if(expression){ do this; } The above argument named 'expression' is basically a condition that we pass into the 'if' and if it returns 'true' then the code block inside it will be executed otherwise n
3 min read
Left Shift Assignment (<<=) Operator in JavaScript
The Left Shift Assignment Operator is represented by "<<=". This operator moves the specified number of bits to the left and assigns that result to the variable. We can fill the vacated place by 0. The left shift operator treats the integer stored in the variable to the operator's left as a 32-bit binary number. This can also be explained as
2 min read
JavaScript ‘===’ vs ‘==’Comparison Operator
In Javascript(ES6), there are four ways to test equality which are listed below: Using '==' operatorUsing '===' operatorSameValueZero: used mainly in sets, maps and arrays.SameValue: used elsewhereNow, our main concern is getting to know the difference between the '==' and '===' operators that the javascript provides, though they look similar, they
3 min read
Arrow operator in ES6 of JavaScript
ES6 has come with various advantages and one of them is the arrow operator. It has reduced the function defining code size so it is one of the trending questions asked in the interview. Let us have a deeper dive into the arrow operator's functioning. Syntax: In ES5 a function is defined by the following syntax: function functionName(arg1, arg2….) {
4 min read
The 'new' operator in Javascript for Error Handling
In this article, we will see new operator usage during error throwing as well as handling the error() method in JavaScript using theoretical as well as coding examples. JavaScript new operator: The new operator is actually a special operator that is used along with the error() method which is the method for the error class used to instantiate any s
3 min read
How to get negative result using modulo operator in JavaScript ?
The %(modulo) operator in JavaScript gives the remainder obtained by dividing two numbers. There is a difference between the %(modulo) and the remainder operator. When remainder or %(modulo) is calculated on positive numbers then both behave similarly but when negative numbers are used then both behave differently. The JavaScript %(modulo) behaves
4 min read
What is JavaScript >>> Operator and how to use it ?
The JavaScript >>> represents the zero-fill right shift operator. It is also called the unsigned right-bit shift operator. It comes under the category of Bitwise operators. Bitwise operators treat operands as 32-bit integer numbers and operate on their binary representation. Zero-fill right shift (>>>) operator: It is a binary ope
3 min read
What does +_ operator mean in JavaScript ?
Unary Operator: A unary operation contain only one operand. Here, the '+' unary plus operator converts its operand to Number type. While it also acts as an arithmetic operator with two operands which returns an addition result on calculation. JavaScript Identifiers: Javascript Identifiers are used to name variables (and keywords, functions, and lab
2 min read
JavaScript Unsigned Right Shift Assignment Operator
In JavaScript ">>>=" is known as the unsigned right shift assignment bitwise operator. This operator is used to move a particular amount of bits to the right and returns a number that is assigned to a variable. Syntax: a >>>= b Meaning: a = a >>> b Return value: It returns the number after shifting of bits. Example 1: Thi
1 min read
JavaScript SyntaxError - Missing name after . operator
This JavaScript exception missing name after . operator occurs if the dot operator (.) is used in the wrong manner for property access. Message: SyntaxError: missing name after . operator Error Type: SyntaxError Cause of Error: The dot operator (.) is used to access the property. Users will have to provide the name of the properties to access. Some
1 min read
JavaScript SyntaxError - Applying the 'delete' operator to an unqualified name is deprecated
This JavaScript exception applying the 'delete' operator to an unqualified name is deprecated works in strict mode and it occurs if variables are tried to be deleted with the delete operator. Message: SyntaxError: Calling delete on expression not allowed in strict mode (Edge) SyntaxError: applying the 'delete' operator to an unqualified name is dep
1 min read
JavaScript TypeError - Cannot use 'in' operator to search for 'X' in 'Y'
This JavaScript exception Cannot use 'in' operator to search for 'X' in 'Y' occurs if in operator is used to search in strings, numbers, or other primitive types. It can not be used other than type-checking. Message: TypeError: Invalid operand to 'in' (Edge) TypeError: right-hand side of 'in' must be an object, got 'x' (Firefox) TypeError: cannot u
1 min read
JavaScript Remainder(%) Operator
The remainder operator in JavaScript is used to get the remaining value when an operand is divided by another operand. In some languages, % is considered modulo. Modulo and Remainder work differently when the sign of both operands is different. In JavaScript remainder takes the sign of the dividend and to get modulo ((a % n) + n) % n should be used
2 min read
Nullish Coalescing Assignment (??=) Operator in JavaScript
This is a new operator introduced by javascript. This operator is represented by x ??= y and it is called Logical nullish assignment operator. Only if the value of x is nullish then the value of y will be assigned to x that means if the value of x is null or undefined then the value of y will be assigned to x. Let's discuss how this logical nullish
2 min read
JavaScript Logical AND assignment (&&=) Operator
This operator is represented by x &&= y, and it is called the logical AND assignment operator. It assigns the value of y into x only if x is a truthy value. We use this operator x &&= y like this. Now break this expression into two parts, x && (x = y). If the value of x is true, then the statement (x = y) executes, and the v
2 min read
JavaScript Logical OR assignment (||=) Operator
This operator is represented by x ||= y and it is called a logical OR assignment operator. If the value of x is falsy then the value of y will be assigned to x. When we divide it into two parts it becomes x || ( x = y ). It checks if x is true or false, if the value of x is falsy then it runs the ( x = y ) block and the value of y gets stored into
2 min read
JavaScript Arithmetic Unary Plus(+) Operator
The Unary plus(+) operation is a single operand operator (which means it worked with only a single operand preceding or succeeding to it), which is used to convert its operand to a number, if it isn't already a number. Syntax: +Operand Below examples illustrate the Unary plus(+) Operator in JavaScript: Example 1:This example shows the use of the Ja
1 min read
JavaScript Arithmetic Unary Negation(-) Operator
The Unary negation(-) operation is a single operand operator (which means it worked with only a single operand preceding or succeeding to it), which is used to convert its operand to a negative number, if it isn't already a negative number. Syntax: -Operand Example 1: This example shows the use of JavaScript Unary negation(-) Operator. C/C++ Code
1 min read
JavaScript Remainder Assignment(%=) Operator
JavaScript remainder assignment operator (%=) assigns the remainder to the variable after dividing a variable by the value of the right operand. Syntax: Operator: x %= y Meaning: x = x % y Below example illustrate the Remainder assignment(%=) Operator in JavaScript: Example 1: The following example demonstrates if the given number is divisible by 4
1 min read
Strict Equality(===) Comparison Operator in JavaScript
JavaScript Strict Equality Operator is used to compare two operands and return true if both the value and type of operands are the same. Since type conversion is not done, so even if the value stored in operands is the same but their type is different the operation will return false. Syntax: a===b Example 1: In this example, we will compare the val
2 min read
Multiplication Assignment(*=) Operator in JavaScript
Multiplication Assignment Operator(*=) in JavaScript is used to multiply two operands and assign the result to the right operand. Syntax: variable1 *= variable2 // variable1 = variable1 * variable2 Example 1: In this example, we multiply two numerical values using the Multiplication Assignment Operator(*=) and assign the result variable in javascri
1 min read
Explain the purpose of the ‘in’ operator in JavaScript
JavaScript in operator is used to check whether the data is within the object or in an array. In an object, the in operator works only on the key or property of the object. If the key or property exists then this operator returns true otherwise false. Similarly, for arrays, it will return true if we pass the index of the element not for a particula
2 min read
How to check a number is even without using modulo operator in JavaScript ?
In this article, we learn how to check if a number is even without using the % or modulo operator. The methods are mentioned below. 1. By using the bitwise '&' operator: When & operation is done on a number with 1 it will return 0 if the number is zero. We will use this method to check if the number is even Explanation: The calculation belo
3 min read
Multiplication(*) Arithmetic Operator in JavaScript
JavaScript arithmetic multiplication operator is used to find the product of operands. Like if we want the multiplication of any two numbers then this operator can be useful. Syntax: a*b Return: It returns the roduct of the operands. Example 1: In this example, we will perform multiplication on Number. C/C++ Code console.log(100*20); console.log(In
1 min read
Division(/) Arithmetic Operator in JavaScript
JavaScript airthmetic division operator is used to find the quotient of operands. The left operator is treated as a dividend and the right operator is treated as a divisor. Syntax: a/bReturn Type: It returns the quotient of the operands Example 1: In this example, we will perform basic division on Numbers. C/C++ Code console.log(100/20); console.lo
1 min read
Bitwise AND Assignment (&=) Operator in JavaScript
The Bitwise AND Assignment Operator is represented by "&=". This operator uses the binary representation of both operands and performs the bitwise AND operation and then assigns the result to the left operand. This can also be explained as applying the logical AND operation to the first operand and second operand and after that assigning the re
1 min read
Exponentiation(**) Arithmetic Operator in JavaScript
JavaScript exponentiation(**) operator in JavaScript is represented by "**" and is used to find the power of the first operator raised to the second operator. This operator is equal to Math.pow() but makes the code simpler and can even accept BigInt primitive data type. The exponentiation operator is right associative which means that x**y**z will
2 min read
Decrement(--) Arithmetic Operator in JavaScript
JavaScript decrement operator is used to decrease the value of the variable by one. The value returned from the operand depends on whether the decrement operator was on the left(prefix decrement) or right(postfix decrement). If the operator is used before the operand then the value is decreased by one and then returned but if the operator is after
2 min read
Subtraction(-) Arithmetic Operator in JavaScript
JavaSscript arithmetic subtraction operator is used to find the difference between operators after subtracting them. Depending on the nature of the two operands, the subtraction operator performs either number subtraction or BigInt subtraction after converting both operands to numeric values. Syntax: a-bReturn Type: It returns a number after subtra
1 min read
three90RightbarBannerImg