---
title: "Use a fallback image | JavaScript maps SDK"
description: "Use a coalesce expression to display a fallback image."
---

> For AI agents: a documentation index is available at [/llms.txt](https://docs.maptiler.com/llms.txt). Markdown versions of all pages are available by appending .md to any URL.

# Use a fallback image

Use a [`coalesce`](https://docs.maptiler.com/gl-style-specification/expressions/#coalesce) expression to display another image when a requested image is not available.

### 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',
    style: MapStyle.STREETS,
    center: [-77, 38.75],
    zoom: 5
});
map.on('load', function () {
    map.addSource('points', {
        'type': 'geojson',
        'data': {
            'type': 'FeatureCollection',
            'features': [
                {
                    'type': 'Feature',
                    'geometry': {
                        'type': 'Point',
                        'coordinates': [
                            -77.03238901390978,
                            38.913188059745586
                        ]
                    },
                    'properties': {
                        'title': 'Washington DC',
                        'icon': 'monument'
                    }
                },
                {
                    'type': 'Feature',
                    'geometry': {
                        'type': 'Point',
                        'coordinates': [-79.9959, 40.4406]
                    },
                    'properties': {
                        'title': 'Pittsburgh',
                        'icon': 'bridges'
                    }
                },
                {
                    'type': 'Feature',
                    'geometry': {
                        'type': 'Point',
                        'coordinates': [-76.2859, 36.8508]
                    },
                    'properties': {
                        'title': 'Norfolk',
                        'icon': 'harbor'
                    }
                }
            ]
        }
    });
    map.addLayer({
        'id': 'points',
        'type': 'symbol',
        'source': 'points',
        'layout': {
            'icon-image': [
                'coalesce',
                ['image', ['concat', ['get', 'icon'], '-15']],
                ['image', 'zoo']
            ],
            'text-field': ['get', 'title'],
            'text-font': ['Open Sans Semibold', 'Arial Unicode MS Bold'],
            'text-offset': [0, 0.6],
            'text-anchor': 'top'
        }
    });
});
```

#### index.html

```html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="robots" content="noindex">
  <title>Use a fallback image | 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>Use a fallback image</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',
        style: maptilersdk.MapStyle.STREETS,
        center: [-77, 38.75],
        zoom: 5
    });
    map.on('load', function () {
        map.addSource('points', {
            'type': 'geojson',
            'data': {
                'type': 'FeatureCollection',
                'features': [
                    {
                        'type': 'Feature',
                        'geometry': {
                            'type': 'Point',
                            'coordinates': [
                                -77.03238901390978,
                                38.913188059745586
                            ]
                        },
                        'properties': {
                            'title': 'Washington DC',
                            'icon': 'monument'
                        }
                    },
                    {
                        'type': 'Feature',
                        'geometry': {
                            'type': 'Point',
                            'coordinates': [-79.9959, 40.4406]
                        },
                        'properties': {
                            'title': 'Pittsburgh',
                            'icon': 'bridges'
                        }
                    },
                    {
                        'type': 'Feature',
                        'geometry': {
                            'type': 'Point',
                            'coordinates': [-76.2859, 36.8508]
                        },
                        'properties': {
                            'title': 'Norfolk',
                            'icon': 'harbor'
                        }
                    }
                ]
            }
        });
        map.addLayer({
            'id': 'points',
            'type': 'symbol',
            'source': 'points',
            'layout': {
                'icon-image': [
                    'coalesce',
                    ['image', ['concat', ['get', 'icon'], '-15']],
                    ['image', 'zoo']
                ],
                'text-field': ['get', 'title'],
                'text-font': ['Open Sans Semibold', 'Arial Unicode MS Bold'],
                'text-offset': [0, 0.6],
                'text-anchor': 'top'
            }
        });
    });
</script>
</body>
</html>
```

## Related examples

### [Icon image URL inside a rich text format label](https://docs.maptiler.com/sdk-js/examples/rich-text-format/)

Examples

Display rich text labels with texts and icons.

### [Filter symbols by text input](https://docs.maptiler.com/sdk-js/examples/filter-markers-by-input/)

Examples

Filter symbols by typing names into a text input.

### [Use locally generated ideographs](https://docs.maptiler.com/sdk-js/examples/local-ideographs/)

Examples

Use the localIdeographFontFamily setting to speed up map load times by using locally available fonts instead of font data fetched from the server.

### [Variable label placement](https://docs.maptiler.com/sdk-js/examples/variable-label-placement/)

Examples

Dynamically shift label positions to keep them visible.
