I'm trying to call the only object in a array but this always returns undefined. if i log the whole array i can see everything in it, if i call the object of it it returns undefined!
The code:
var pos = [];
this code is inside a function____
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
pos.latlng = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
console.log(position.coords.latitude);
});
}
console.log(pos.latlng); <--- undefined
console.log(pos); <--- works fine
console.log(pos[0].latlng); <--- also tried this
Result of console.log(pos);
Object
latlng : Object
lat : 52.136624499999996
lng : 5.3469106
__proto__:Object
__proto__:Object
posis an array not an object. The way you're using it it should be declared like:var pos = {}.console.log(pos); <--- works fine.