Loop in return statement of a function in JavaScript

I was trying to built a heat map using google map and I write this code but now as you can see I am passing hard coded values to it, I want to use a loop in return statement because I am using MySQL and all the info will be stored in the database so when I call this function it should exactly create as much points on the map as many values are stored in the database.

function getPoints() 
    return [
        new google.maps.LatLng(28.81820630, 77.05907876),
        new google.maps.LatLng(28.8097835, 77.05673218),
        new google.maps.LatLng(28.8097835, 77.003688),
        new google.maps.LatLng(28.8097835, 77.002815),
        new google.maps.LatLng(28.782992, 77.002112),
        new google.maps.LatLng(28.783100, 77.001061),
        new google.maps.LatLng(28.783206, 77.000829),
        new google.maps.LatLng(28.783273, 77.000320),
        new google.maps.LatLng(28.783316, 77.000023),
        new google.maps.LatLng(28.783357, 77.039790),
        new google.maps.LatLng(28.783281, 77.039687),
        new google.maps.LatLng(28.783368, 77.039666),
        new google.maps.LatLng(28.783383, 77.039590),
        new google.maps.LatLng(28.783508, 77.039525),
        new google.maps.LatLng(28.783802, 77.039591),
        new google.maps.LatLng(28.780107, 77.039668),
        new google.maps.LatLng(28.780206, 77.039686),
        new google.maps.LatLng(28.780386, 77.039790),
        new google.maps.LatLng(28.780701, 77.039902),
        new google.maps.LatLng(28.780965, 77.039938),
        new google.maps.LatLng(28.785010, 77.039907),
        new google.maps.LatLng(28.785360, 77.039952),
        new google.maps.LatLng(28.785715, 77.000030),
        new google.maps.LatLng(28.786117, 77.000119),
        new google.maps.LatLng(28.786560, 77.000209),
        new google.maps.LatLng(28.786905, 77.000270),
        new google.maps.LatLng(29.786905, 77.000270)

Can we use loop statements in JavaScript in return statement ? If so then how?


No, it is not possible. But why you don’t first build the array and afterwards return it?

function getPoints() {
    var array = [];
    for (...) {
        array.push(new google.maps.LatLng(value1, value2));
    return array;

Leave a Reply

Your email address will not be published. Required fields are marked *