A for…in loop is a special loop in JavaScript that enumerates all the keys (object property names) of an object. With each new iteration, a new property string is assigned to the loop variable.
Error:
Uncaught ReferenceError: Object property is not defined
If we try to compare this loop variable with a non-string variable a ReferenceError is generated which can be solved simply by using a variable in string format while comparing it with the for…in loop variable. This error can happen in two ways discussed below:
Example 1: Comparing the for…in loop variable with the non-string variable
Here we have created an object ‘neveropen’. We used for…in the loop to look for a property named ‘about’ and log it to the console. But here the about is not in string format, Hence we will get a Reference Error in this code.
Javascript
<script> // A neveropen object const neveropen = { about: "Computer Science portal for neveropen", problems_count: 2690, used_by: ['Professionals', 'Students'], used_for: ['DSA Practice', ' Articles & Editorials'] } for (key in neveropen) { if (key === about) { // ReferenceError:'about' is not defined console.log(`neveropen is a ${neveropen[key]}.`) break; } } </script> |
Output:
ReferenceError: about is not defined
Solution: In the above example, simply converting ‘about‘ to the string will remove the Reference Error from the code.
Javascript
<script> // A neveropen object const neveropen = { about: "Computer Science portal for neveropen", problems_count: 2690, used_by: ['Professionals', 'Students'], used_for: ['DSA Practice', ' Articles & Editorials'] } for (key in neveropen) { if (key === "about") { // Used string to compare with // loop variable console.log(`neveropen is a ${neveropen[key]}.`) break; } } </script> |
Output:
Example 2: Non-string arguments to the function
This error can also happen if we passed non-string arguments to the function, which later will be used in comparison with the loop variable. In the below code, we used the non-string argument ‘used_for’ while calling the function ‘fun’.
Javascript
<script> // A neveropen object const neveropen = { Desc: "Computer Science portal for neveropen", Problems_count: 2690, used_by: ['Professionals', 'Students'], used_for: ['DSA Practice', ' Articles & Editorials'] } // Call the gfg method fun(neveropen, used_for) // A method to console info about neveropen function fun(obj, uses) { for (key in obj) { if (key === uses) { console.log(`neveropen is used for ${obj[uses]}.`) } } } </script> |
Output:
ReferenceError: used_for is nor defined
Solution: Passing the argument ‘used_for’ in string format while calling the function ‘fun’ will solve the Error.
Javascript
<script> // A neveropen object const neveropen = { Desc: "Computer Science portal for neveropen", Problems_count: 2690, used_by: ['Professionals', 'Students'], used_for: ['DSA Practice', ' Articles & Editorials'] } // Call the gfg method fun(neveropen, "used_for") // A method to console info about neveropen function fun(obj, uses) { for (key in obj) { if (key === uses) { console.log(`neveropen is used for ${obj[uses]}.`) } } } </script> |
Output:
