Skip to content
Advertisement

Variable will not become global

I have a variable in a function and I want to call it outside of the function however it says ‘Name is not defined’. Please advise on how I can make this variable global.

JavaScript

Advertisement

Answer

You may want to take a look at variable scope.

The reason you can’t access it it’s because it’s defined inside the .then() lambda function and can only be accessed there.

To have it available outside that function you need to declare it outside it:

JavaScript

Update after your comment:

You don’t see the value in your console.log because the value is assigned inside a Promise, which is never run right when declared.

To console.log the Name effectively, you need to chain another .then() and log it there:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement