im having trouble returning an array that contains decimal values to a variable:
function foo() {
var coords = new Array(39.3,18.2);
console.log(coords[1]); //successfully logs 18.2
return coords;
}
but then…
var result = foo();
alert(result[0]);
that last one throws this error:
Uncaught TypeError: Cannot read property ‘0’ of undefined
As other people mentioned, alert is a function and needs parenthesis
That said, there are some extra points to note:
1) Use array literal syntax instead of
new ArrayIts faster and new Array has some edge cases.
2) Most browsers have developer tools (usually reacheable by F12). This allows you to use the much more convenient console.log instead of alert.