JavaScript for...in loop not working - Object property is not defined Error
Last Updated :
28 Apr, 2025
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>
// A GeeksforGeeks object
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) {
// ReferenceError:'about' is not defined
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>
// A GeeksforGeeks object
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") {
// Used string to compare with
// loop variable
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>
// A GeeksforGeeks object
const GeeksforGeeks = {
Desc: "Computer Science portal for geeks",
Problems_count: 2690,
used_by: ['Professionals', 'Students'],
used_for: ['DSA Practice', ' Articles & Editorials']
}
// Call the gfg method
fun(GeeksforGeeks, used_for)
// A method to console info about GeeksforGeeks
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>
// A GeeksforGeeks object
const GeeksforGeeks = {
Desc: "Computer Science portal for geeks",
Problems_count: 2690,
used_by: ['Professionals', 'Students'],
used_for: ['DSA Practice', ' Articles & Editorials']
}
// Call the gfg method
fun(GeeksforGeeks, "used_for")
// A method to console info about GeeksforGeeks
function fun(obj, uses) {
for (key in obj) {
if (key === uses) {
console.log(`GeeksforGeeks is
used for ${obj[uses]}.`)
}
}
}
</script>
Output:
Similar Reads
Disadvantages of using for..in loop in JavaScript In this article, we will see what are the disadvantages of using for..in loop and how to solve those issues. Disadvantages of using for..in loop: Reason 1: When you add a property in an array or object using the prototype and any other array arr that has no relation with that property when you itera
2 min read
Nesting For Loops in JavaScript Nesting for loops is crucial in JavaScript, enabling iteration over multi-dimensional data structures or performing complex tasks. It involves placing one loop inside another, where the outer loop executes for each iteration of the inner loop. This facilitates operations on multi-dimensional arrays
4 min read
Differences Between for-in and for-of Statement in JavaScript The for..in loop is designed for iterating over an object's keys or property names, making it useful when accessing each property in an object. Although it can also be used to iterate over the indices of an array. Conversely, the for..of loop is intended to iterate directly over values in iterable c
2 min read
How to use forEach with an Array of Objects in JavaScript ? Using the forEach() method with an array of objects in JavaScript is essential for iterating over collections and performing operations on each object. This guide explores effective techniques to utilize forEach() for array manipulation, enhancing your coding skills. Syntax: array.forEach( function(
3 min read
How to Iterate JavaScript Object Containing Array and Nested Objects ? JavaScript provides us with several built-in methods through which one can iterate over the array and nested objects using the for...in loop, Object.keys(), Object.entries(), and Object.values(). Each method serves a distinct purpose in iterating over object properties, keys, and values which are ex
3 min read
JavaScript Error Object Complete Reference Error objects are arising at runtime errors. The error object also uses as the base object for the exceptions defined by the user. The complete list of JavaScript Error Object properties are listed below: Error types JavaScript RangeError â Invalid dateJavaScript RangeError â Repeat count must be no
3 min read