Saturday, September 6, 2025
HomeLanguagesJavascriptJavaScript for…in loop not working – Object property is not defined Error

JavaScript for…in loop not working – Object property is not defined Error

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:

 

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6639 POSTS0 COMMENTS
Nicole Veronica
11803 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11868 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS