How to access a variable defined in a function from with in a nested function in javascript

I have defined a variable inside a function. Inside that function I have defined another function within which I intend to access the variable I defined earlier.

The variable I declared is of type object. When I reference the variable in the nested function and try to access it’s properties, it shows me undefined. When I try to log the value of the function to the console it gives me this response [object Object]. I have done some reading and I have found out that that is a result of the toString() function.

My question is…
How can I access the variable in in a nested function properly and in a clean way.
If the source code is required I can provide it.

Hallo @Elijah_Okello and welcome to the community :wave:

It would be helpful, if you could show a running example of your code. You can use something like or post a link to an example on your own server, of course.

Regarding the logging: Normally, if you just log an object with


browsers show the content of the object. If you log an object together with some text like

console.log('Result: ' + test);

the object gets implicitly converted to a string by the toString() method. This gives

Result: [object Object]

In this case you could use JSON.stringify() to see the content of the object:

console.log('Result: ' + JSON.stringify(test));
Result: {"a":15,"b":"Hello"}

Have a nice day!