---
title: "Visualize population density | JavaScript maps SDK"
description: "Calculate and display population density using variable expressions."
---

# Visualize population density

Use a [variable binding expression](https://docs.maptiler.com/gl-style-specification/expressions/#variable-binding) to calculate and display population density.

In this example, we use the population and area variables to create a choropleth map that visually represents the population density.

### NPM module setup

```bash
npm install --save @maptiler/sdk
```

#### main.js

```js
import { Map, MapStyle, config } from '@maptiler/sdk';
import '@maptiler/sdk/dist/maptiler-sdk.css';

config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
const map = new Map({
    container: 'map', // container id
    style: MapStyle.STREETS,  // stylesheet location
    center: [30.0222, -1.9596], // starting position [lng, lat]
    zoom: 7 // starting zoom
});

map.on('load', function () {
    map.addSource('rwanda-provinces', {
        'type': 'geojson',
        'data':
            'https://docs.maptiler.com/sdk-js/assets/rwanda-provinces.geojson'
    });
    map.addLayer({
        'id': 'rwanda-provinces',
        'type': 'fill',
        'source': 'rwanda-provinces',
        'layout': {},
        'paint': {
            'fill-color': [
                'let',
                'density',
                ['/', ['get', 'population'], ['get', 'sq-km']],
                [
                    'interpolate',
                    ['linear'],
                    ['zoom'],
                    8,
                    [
                        'interpolate',
                        ['linear'],
                        ['var', 'density'],
                        274,
                        ['to-color', '#edf8e9'],
                        1551,
                        ['to-color', '#006d2c']
                    ],
                    10,
                    [
                        'interpolate',
                        ['linear'],
                        ['var', 'density'],
                        274,
                        ['to-color', '#eff3ff'],
                        1551,
                        ['to-color', '#08519c']
                    ]
                ]
            ],
            'fill-opacity': 0.7
        }
    });
});
```

#### index.html

```html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="robots" content="noindex">
  <title>Visualize population density | NPM example</title>
  <link rel="stylesheet" href="./style.css">
</head>
<body>
  <div id="map"></div>
  <script type="module" src="./main.js"></script>
</body>
</html>
```

#### style.css

```css
body {margin: 0; padding: 0;}
#map {position: absolute; top: 0; bottom: 0; width: 100%;}
```

### Basic JavaScript setup

```html
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Visualize population density</title>
<script src="https://cdn.maptiler.com/maptiler-sdk-js/v4.2.0/maptiler-sdk.umd.min.js"></script>
<link href="https://cdn.maptiler.com/maptiler-sdk-js/v4.2.0/maptiler-sdk.css" rel="stylesheet" />
<style>
  body {margin: 0; padding: 0;}
  #map {position: absolute; top: 0; bottom: 0; width: 100%;}
</style>
</head>
<body>
<div id="map"></div>
<script>
    maptilersdk.config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
    const map = new maptilersdk.Map({
        container: 'map', // container id
        style: maptilersdk.MapStyle.STREETS,  // stylesheet location
        center: [30.0222, -1.9596], // starting position [lng, lat]
        zoom: 7 // starting zoom
    });

    map.on('load', function () {
        map.addSource('rwanda-provinces', {
            'type': 'geojson',
            'data':
                'https://docs.maptiler.com/sdk-js/assets/rwanda-provinces.geojson'
        });
        map.addLayer({
            'id': 'rwanda-provinces',
            'type': 'fill',
            'source': 'rwanda-provinces',
            'layout': {},
            'paint': {
                'fill-color': [
                    'let',
                    'density',
                    ['/', ['get', 'population'], ['get', 'sq-km']],
                    [
                        'interpolate',
                        ['linear'],
                        ['zoom'],
                        8,
                        [
                            'interpolate',
                            ['linear'],
                            ['var', 'density'],
                            274,
                            ['to-color', '#edf8e9'],
                            1551,
                            ['to-color', '#006d2c']
                        ],
                        10,
                        [
                            'interpolate',
                            ['linear'],
                            ['var', 'density'],
                            274,
                            ['to-color', '#eff3ff'],
                            1551,
                            ['to-color', '#08519c']
                        ]
                    ]
                ],
                'fill-opacity': 0.7
            }
        });
    });
</script>
</body>
</html>
```

## Related examples

### [Choropleth GeoJSON](https://docs.maptiler.com/sdk-js/examples/choropleth-geojson/)

Tutorials

Create a styled choropleth map using a GeoJSON overlay.

### [Polygon layer (polygon helper)](https://docs.maptiler.com/sdk-js/examples/helper-polygon-minimal/)

Examples

Add minimal polygon layer using MapTiler polygon helper.

### [Interactive choropleth map](https://docs.maptiler.com/sdk-js/examples/interactive-choropleth/)

Examples

Use events and feature states to create a interactive choropleth map.

### [GeoJSON polygon layer](https://docs.maptiler.com/sdk-js/examples/geojson-polygon/)

Tutorials

Add GeoJSON polygon overlay to your web map application.
