Explain the purpose of the ‘in’ operator in JavaScript Last Updated : 09 Jan, 2023 Comments Improve Suggest changes Like Article Like Report 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 particular value. The below examples will help you understand the uses of the operator. Example 1: Using the in operator with objects. Let's create an object first, to create an object we have to assign key-value pairs. const object = { User: 'Geek', Website: 'GeeksforGeeks', Language: 'JavaScript' }; We will check whether the key exists in the object or not by using the in operator. JavaScript <script> const object = { User: 'Geek', Website: 'GeeksforGeeks', Language: 'JavaScript', }; if ('User' in object) { console.log("Found"); } else { console.log("Not found"); } </script> Output: We have checked if the 'User' key is in the object or not, if it is in the object then print Found otherwise Not found. Found Example 2: Using the in operator with arrays. Let's create an array first, to create an array we have to assign values to the array in square brackets. const array1 = ["Geek", "GeeksforGeeks", "JavaScript"]; We will check whether the index value exists in the array or not, JavaScript <script> const array1 = ["Geek", "GeeksforGeeks", "JavaScript"]; if (1 in array1) { console.log("Found: ", array1[1]); } else { console.log("Not found"); } </script> Output: We have checked if index 1 is present in the array or not. Found: GeeksforGeeks Comment More infoAdvertise with us Next Article Explain the purpose of the ‘in’ operator in JavaScript A anuragsingh1022 Follow Improve Article Tags : JavaScript Web Technologies javascript-operators JavaScript-Questions Similar Reads What does OR Operator || in a Statement in JavaScript ? JavaScript is a dynamic programming language that allows developers to write complex code with ease. One of the fundamental concepts in JavaScript is the use of operators, which are symbols that perform operations on one or more values. One such operator is the || (logical OR) operator, which can be 6 min read Right Shift Assignment(>>=) Operator in JavaScript The Right Shift Assignment Operator is represented by ">>=". This operator shifts the first operand to the right and assigns the result to the variable. It can also be explained as shifting the first operand to the right in a specified amount of bits which is the second operand integer and the 1 min read Remainder Assignment(%=) Operator in Javascript The Remainder Assignment Operator in javascript is represented by "%=". This operator is used to divide the value of the variable by another operand and assign the remainder to the variable which is the first operand. This can be also explained as assigning the remainder to the first operand which i 1 min read What is (~~) "double tilde" operator in JavaScript ? This is a special kind of operator in JavaScript. To understand the double tilde operator, first, we need to discuss the tilde operator or Bitwise NOT. The (~) tilde operator takes any number and inverts the binary digits, for example, if the number is (100111) after inversion it would be (011000). 3 min read JavaScript in and instanceof operators JavaScript Relational Operators are used to compare their operands and determine the relationship between them. They return a Boolean value (true or false) based on the comparison result.JavaScript in OperatorThe in-operator in JavaScript checks if a specified property exists in an object or if an e 3 min read What is the Infinity Global Property in JavaScript ? In JavaScript, the Infinity global property represents positive infinity, which is a numeric value that is greater than any other numeric value. It can be used to represent values that are too large to be represented with the Number data type or as a result of operations like division by zero. Also, 2 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 b 1 min read Addition Assignment (+=) Operator in Javascript JavaScript Addition assignment operator(+=) adds a value to a variable, The Addition Assignment (+ =) Sums up left and right operand values and then assigns the result to the left operand. The two major operations that can be performed using this operator are the addition of numbers and the concaten 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 ( 2 min read Like