JavaScript for…in loop not working – Object property is not defined Error
Last Updated :
07 Nov, 2022
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 ‘GeeksforGeeks’. 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>
const GeeksforGeeks = {
about: "Computer Science portal for geeks" ,
problems_count: 2690,
used_by: [ 'Professionals' , 'Students' ],
used_for: [ 'DSA Practice' , ' Articles & Editorials' ]
}
for (key in GeeksforGeeks) {
if (key === about) {
console.log(`GeeksforGeeks is a
${GeeksforGeeks[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>
const GeeksforGeeks = {
about: "Computer Science portal for geeks" ,
problems_count: 2690,
used_by: [ 'Professionals' , 'Students' ],
used_for: [ 'DSA Practice' , ' Articles & Editorials' ]
}
for (key in GeeksforGeeks) {
if (key === "about" ) {
console.log(`GeeksforGeeks is
a ${GeeksforGeeks[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>
const GeeksforGeeks = {
Desc: "Computer Science portal for geeks" ,
Problems_count: 2690,
used_by: [ 'Professionals' , 'Students' ],
used_for: [ 'DSA Practice' , ' Articles & Editorials' ]
}
fun(GeeksforGeeks, used_for)
function fun(obj, uses) {
for (key in obj) {
if (key === uses) {
console.log(`GeeksforGeeks 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>
const GeeksforGeeks = {
Desc: "Computer Science portal for geeks" ,
Problems_count: 2690,
used_by: [ 'Professionals' , 'Students' ],
used_for: [ 'DSA Practice' , ' Articles & Editorials' ]
}
fun(GeeksforGeeks, "used_for" )
function fun(obj, uses) {
for (key in obj) {
if (key === uses) {
console.log(`GeeksforGeeks is
used for ${obj[uses]}.`)
}
}
}
</script>
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...