Local and Global variables, arrow functions

I have an arrow function working, it calculates the nearest point (to My location) and the distance to it.
The problem is I cannot access its variables outside the function as they are local by definition. I need them to be avaliable externally or at least read them outside the function.
I tried export (I already have a default export) and globally declaring it but without success.

var marK = [
    { latitude: -34.906874, longitude: -56.206150 },   //  Globally declared
    { latitude: -34.888571, longitude: -56.191530 },
    { latitude: -34.880967, longitude: -56.163187 },
    { latitude: -34.895048, longitude: -56.155973 },
    { latitude: -34.911097, longitude: -56.133299 },
    { latitude: -34.920388, longitude: -56.146530 },
   ];

const finder = () => {
  var firstfind = findNearest({ latitude: coords.latitude, longitude: coords.longitude }, marK);

var found = getPreciseDistance(
    { latitude: coords.latitude, longitude: coords.longitude },
    { latitude: firstfind.latitude, longitude: firstfind.longitude }
);
   console.log(found)
   alert( JSON.stringify(found));           //I can't use var "found" in my js

}                          

You can’t use found outside the scope in which it has been defined, true - but you can return the thing it contains.

Your finder is a function, and if you tell that function to return a value, you can then place that returned value into a variable outside that functions scope.

1 Like

Thanks Snowmonkey, I added the return found; in the function and outside the function’s scope var globfound = found;. I’m getting “blank” as a result (at least no errors! I tried JSON.stringify() too).
Sorry I must be doing something wrong.

1 Like

you need to call the respective function to receive whatever it can return. Just like you assign to found the value of getPreciseDistance(). getPrecideDistance appears to be a function, which takes some arguments and returns a value, which value you assign to your found variable.

function test(){
  let localVar='get me out'
  return localVar
}

let outside=test()

console.log(outside)  //logs 'get me out'
2 Likes

Thank you Sylvant! Your solution to reuse the local variable works. I tried it as you wrote it and no problem, it shows the variable outside scope but when I replace it with my function it crashes, I get null is not an object.

function finder(){
  var firstfind = findNearest({ latitude: coords.latitude, longitude: coords.longitude }, mK);
  let found = getPreciseDistance(
    { latitude: coords.latitude, longitude: coords.longitude },
    { latitude: firstfind.latitude, longitude: firstfind.longitude }
);
return found
}
let outside=finder()
console.log(outside)
1 Like

actually its snowmoneky who suggested that solution, i just presented the basic way on how to return a value from a function and make use of it outside, which is a basic thing and if you are working with functions and DOM, you should be comfortable with such manipulations.
It looks like you lack some basic udnerstanding regarding functions and DOM manipulations and before you proceed work on those projects you might wanna visit some course to get more comfortable with JS. There are many free out there and good ones.

2 Likes