Filter a feature layer with SQL

Learn how to use a SQL query to limit features displayed in a feature layer.

A hosted feature layer can contain a large number of features. To display a subset of the features, you can filter features on the server-side with a definition expression. Definition expressions are different than feature layer queries: they only support a SQL where clause without a geometry (spatial) parameter, and are only used to filter features at the time they are displayed in a map or scene. They cannot be used to get features like a feature layer query.

In this tutorial, you apply a server-side SQL query with a definitionExpression to filter the LA County Parcels feature layer .

Prerequisites

You need a free ArcGIS developer account to access your dashboard and API keys. The API key must be scoped to access the services used in this tutorial.

Steps

Create a new pen

  1. To get started, either complete the Display a map tutorial or .

Set the API key

To access ArcGIS services, you need an API key.

  1. Go to your dashboard to get an API key.

  2. In CodePen, set the apiKey to your key, so it can be used to access basemap layer and location services.

    Change line
        
    1
    2
    3
    4
    esriConfig.apiKey = "YOUR_API_KEY";
    const map = new Map({
      basemap: "arcgis-topographic" // Basemap layer service
    });
    

Add modules

  1. In the require statement, add the FeatureLayer module.

    The ArcGIS API for JavaScript uses AMD modules. The require function is used to load modules so they can be used in the main function. It's important to keep the module references and function parameters in the same order.

    Add line.Change line
                                                                                             
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    <html>
    <head>
      <meta name="description" content="ArcGIS JavaScript Tutorials: Filter a feature layer">
      <meta charset="utf-8">
      <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
      <title>ArcGIS API for JavaScript Tutorials: Filter a feature layer with SQL (JSAPI)</title>
      <style>
        html,
        body,
        #viewDiv {
          padding: 0;
          margin: 0;
          height: 100%;
          width: 100%;
      </style>
      <link rel="stylesheet" href="https://js.arcgis.com/4.20/esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.20/"></script>
    </head>
    
    <script>
      require([
          "esri/config",
          "esri/Map",
          "esri/views/MapView",
    
          "esri/layers/FeatureLayer"
    
        ], function(esriConfig,Map,MapView,FeatureLayer) {
          esriConfig.apiKey = "YOUR_API_KEY";
          const map = new Map({
            basemap: "arcgis-topographic" // Basemap layer service
          const view = new MapView({
            container: "viewDiv",
            map: map,
            center: [-118.80543,34.02700], // Longitude, latitude
            zoom: 12
          // Create a UI with the filter expressions
          const sqlExpressions = ["Choose a SQL where clause...", "Roll_LandValue < 200000", "TaxRateArea = 10853", "Bedrooms5 > 0", "UseType = 'Residential'", "Roll_RealEstateExemp > 0"];
          // UI
          const selectFilter = document.createElement("select");
          selectFilter.setAttribute("class", "esri-widget esri-select");
          selectFilter.setAttribute("style", "width: 275px; font-family: Avenir Next W00; font-size: 1em;");
          sqlExpressions.forEach(function(sql){
            let option = document.createElement("option");
          view.ui.add(selectFilter, "top-right");
          // Add a feature layer to map with all features visible on client (no filter)
          const featureLayer = new FeatureLayer({
            url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0",
            outFields: ["*"],
            popupTemplate: {
              title: "{UseType}",
              content: "Description: {UseDescription}. Land value: {Roll_LandValue}"
            definitionExpression: "1=0"
          // Server-side filter
          function setFeatureLayerFilter(expression) {
          // Event listener
          selectFilter.addEventListener('change', function (event) {
      </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>

Create a selector

Use an HTML select element to provide a list of SQL queries for the LA County Parcels feature layer.

  1. Create a sqlExpressions array of SQL queries.

    Add line.Add line.
                                                                                             
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    <html>
    <head>
      <meta name="description" content="ArcGIS JavaScript Tutorials: Filter a feature layer">
      <meta charset="utf-8">
      <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
      <title>ArcGIS API for JavaScript Tutorials: Filter a feature layer with SQL (JSAPI)</title>
      <style>
        html,
        body,
        #viewDiv {
          padding: 0;
          margin: 0;
          height: 100%;
          width: 100%;
      </style>
      <link rel="stylesheet" href="https://js.arcgis.com/4.20/esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.20/"></script>
    </head>
    
    <script>
      require([
          "esri/config",
          "esri/Map",
          "esri/views/MapView",
          "esri/layers/FeatureLayer"
        ], function(esriConfig,Map,MapView,FeatureLayer) {
          esriConfig.apiKey = "YOUR_API_KEY";
          const map = new Map({
            basemap: "arcgis-topographic" // Basemap layer service
          const view = new MapView({
            container: "viewDiv",
            map: map,
            center: [-118.80543,34.02700], // Longitude, latitude
            zoom: 12
          });
    
          // Create a UI with the filter expressions
          const sqlExpressions = ["Choose a SQL where clause...", "Roll_LandValue < 200000", "TaxRateArea = 10853", "Bedrooms5 > 0", "UseType = 'Residential'", "Roll_RealEstateExemp > 0"];
          // UI
          const selectFilter = document.createElement("select");
          selectFilter.setAttribute("class", "esri-widget esri-select");
          selectFilter.setAttribute("style", "width: 275px; font-family: Avenir Next W00; font-size: 1em;");
          sqlExpressions.forEach(function(sql){
            let option = document.createElement("option");
          view.ui.add(selectFilter, "top-right");
          // Add a feature layer to map with all features visible on client (no filter)
          const featureLayer = new FeatureLayer({
            url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0",
            outFields: ["*"],
            popupTemplate: {
              title: "{UseType}",
              content: "Description: {UseDescription}. Land value: {Roll_LandValue}"
            definitionExpression: "1=0"
          // Server-side filter
          function setFeatureLayerFilter(expression) {
          // Event listener
          selectFilter.addEventListener('change', function (event) {
      </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>
  2. Create a parent selectFilter element and add option elements for each SQL query. Add some css styles to the elements.

    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
                                                                                             
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    <html>
    <head>
      <meta name="description" content="ArcGIS JavaScript Tutorials: Filter a feature layer">
      <meta charset="utf-8">
      <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
      <title>ArcGIS API for JavaScript Tutorials: Filter a feature layer with SQL (JSAPI)</title>
      <style>
        html,
        body,
        #viewDiv {
          padding: 0;
          margin: 0;
          height: 100%;
          width: 100%;
      </style>
      <link rel="stylesheet" href="https://js.arcgis.com/4.20/esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.20/"></script>
    </head>
    
    <script>
      require([
          "esri/config",
          "esri/Map",
          "esri/views/MapView",
          "esri/layers/FeatureLayer"
        ], function(esriConfig,Map,MapView,FeatureLayer) {
          esriConfig.apiKey = "YOUR_API_KEY";
          const map = new Map({
            basemap: "arcgis-topographic" // Basemap layer service
          const view = new MapView({
            container: "viewDiv",
            map: map,
            center: [-118.80543,34.02700], // Longitude, latitude
            zoom: 12
          // Create a UI with the filter expressions
          const sqlExpressions = ["Choose a SQL where clause...", "Roll_LandValue < 200000", "TaxRateArea = 10853", "Bedrooms5 > 0", "UseType = 'Residential'", "Roll_RealEstateExemp > 0"];
    
          // UI
          const selectFilter = document.createElement("select");
          selectFilter.setAttribute("class", "esri-widget esri-select");
          selectFilter.setAttribute("style", "width: 275px; font-family: Avenir Next W00; font-size: 1em;");
    
          sqlExpressions.forEach(function(sql){
            let option = document.createElement("option");
            option.value = sql;
            option.innerHTML = sql;
            selectFilter.appendChild(option);
          });
    
          view.ui.add(selectFilter, "top-right");
          // Add a feature layer to map with all features visible on client (no filter)
          const featureLayer = new FeatureLayer({
            url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0",
            outFields: ["*"],
            popupTemplate: {
              title: "{UseType}",
              content: "Description: {UseDescription}. Land value: {Roll_LandValue}"
            definitionExpression: "1=0"
          // Server-side filter
          function setFeatureLayerFilter(expression) {
          // Event listener
          selectFilter.addEventListener('change', function (event) {
      </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>
  3. Add selectFilter to the top-right corner of the view.

    Add line.
                                                                                             
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    <html>
    <head>
      <meta name="description" content="ArcGIS JavaScript Tutorials: Filter a feature layer">
      <meta charset="utf-8">
      <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
      <title>ArcGIS API for JavaScript Tutorials: Filter a feature layer with SQL (JSAPI)</title>
      <style>
        html,
        body,
        #viewDiv {
          padding: 0;
          margin: 0;
          height: 100%;
          width: 100%;
      </style>
      <link rel="stylesheet" href="https://js.arcgis.com/4.20/esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.20/"></script>
    </head>
    
    <script>
      require([
          "esri/config",
          "esri/Map",
          "esri/views/MapView",
          "esri/layers/FeatureLayer"
        ], function(esriConfig,Map,MapView,FeatureLayer) {
          esriConfig.apiKey = "YOUR_API_KEY";
          const map = new Map({
            basemap: "arcgis-topographic" // Basemap layer service
          const view = new MapView({
            container: "viewDiv",
            map: map,
            center: [-118.80543,34.02700], // Longitude, latitude
            zoom: 12
          // Create a UI with the filter expressions
          const sqlExpressions = ["Choose a SQL where clause...", "Roll_LandValue < 200000", "TaxRateArea = 10853", "Bedrooms5 > 0", "UseType = 'Residential'", "Roll_RealEstateExemp > 0"];
          // UI
          const selectFilter = document.createElement("select");
          selectFilter.setAttribute("class", "esri-widget esri-select");
          selectFilter.setAttribute("style", "width: 275px; font-family: Avenir Next W00; font-size: 1em;");
          sqlExpressions.forEach(function(sql){
            let option = document.createElement("option");
            option.value = sql;
            option.innerHTML = sql;
            selectFilter.appendChild(option);
          });
    
          view.ui.add(selectFilter, "top-right");
          // Add a feature layer to map with all features visible on client (no filter)
          const featureLayer = new FeatureLayer({
            url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0",
            outFields: ["*"],
            popupTemplate: {
              title: "{UseType}",
              content: "Description: {UseDescription}. Land value: {Roll_LandValue}"
            definitionExpression: "1=0"
          // Server-side filter
          function setFeatureLayerFilter(expression) {
          // Event listener
          selectFilter.addEventListener('change', function (event) {
      </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>
  4. Verify that the select element is created.

Create a feature layer to filter

Use the FeatureLayer class to access the LA County Parcels feature layer. Since you are performing a server-side query, the feature layer does not need to be added to the map. However, to view the results of the query, the feature layer will be added to the map.

  1. Create a featureLayer and set the url property to access the feature layer in the feature service. Set the outFields property to return all attributes on the client and the popupTemplate to display the parcel description and land value. Set the definitionExpression to 1=0 so no features are displayed when the layer is loaded. Add featureLayer to the map.

    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
                                                                                             
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    <html>
    <head>
      <meta name="description" content="ArcGIS JavaScript Tutorials: Filter a feature layer">
      <meta charset="utf-8">
      <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
      <title>ArcGIS API for JavaScript Tutorials: Filter a feature layer with SQL (JSAPI)</title>
      <style>
        html,
        body,
        #viewDiv {
          padding: 0;
          margin: 0;
          height: 100%;
          width: 100%;
      </style>
      <link rel="stylesheet" href="https://js.arcgis.com/4.20/esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.20/"></script>
    </head>
    
    <script>
      require([
          "esri/config",
          "esri/Map",
          "esri/views/MapView",
          "esri/layers/FeatureLayer"
        ], function(esriConfig,Map,MapView,FeatureLayer) {
          esriConfig.apiKey = "YOUR_API_KEY";
          const map = new Map({
            basemap: "arcgis-topographic" // Basemap layer service
          const view = new MapView({
            container: "viewDiv",
            map: map,
            center: [-118.80543,34.02700], // Longitude, latitude
            zoom: 12
          // Create a UI with the filter expressions
          const sqlExpressions = ["Choose a SQL where clause...", "Roll_LandValue < 200000", "TaxRateArea = 10853", "Bedrooms5 > 0", "UseType = 'Residential'", "Roll_RealEstateExemp > 0"];
          // UI
          const selectFilter = document.createElement("select");
          selectFilter.setAttribute("class", "esri-widget esri-select");
          selectFilter.setAttribute("style", "width: 275px; font-family: Avenir Next W00; font-size: 1em;");
          sqlExpressions.forEach(function(sql){
            let option = document.createElement("option");
          view.ui.add(selectFilter, "top-right");
    
          // Add a feature layer to map with all features visible on client (no filter)
          const featureLayer = new FeatureLayer({
            url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0",
            outFields: ["*"],
            popupTemplate: {
              title: "{UseType}",
              content: "Description: {UseDescription}. Land value: {Roll_LandValue}"
            },
            definitionExpression: "1=0"
          });
          map.add(featureLayer);
    
          // Server-side filter
          function setFeatureLayerFilter(expression) {
          // Event listener
          selectFilter.addEventListener('change', function (event) {
      </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>

Apply the SQL expression

The definitionExpression is the SQL where clause. Use the expression to apply a filter and limit features displayed in the map.

  1. Create a setFeatureLayerFilter function with an expression parameter. Set the definitionExpression to filter the feature layer with the expression.

    Add line.Add line.Add line.Add line.
                                                                                             
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    <html>
    <head>
      <meta name="description" content="ArcGIS JavaScript Tutorials: Filter a feature layer">
      <meta charset="utf-8">
      <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
      <title>ArcGIS API for JavaScript Tutorials: Filter a feature layer with SQL (JSAPI)</title>
      <style>
        html,
        body,
        #viewDiv {
          padding: 0;
          margin: 0;
          height: 100%;
          width: 100%;
      </style>
      <link rel="stylesheet" href="https://js.arcgis.com/4.20/esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.20/"></script>
    </head>
    
    <script>
      require([
          "esri/config",
          "esri/Map",
          "esri/views/MapView",
          "esri/layers/FeatureLayer"
        ], function(esriConfig,Map,MapView,FeatureLayer) {
          esriConfig.apiKey = "YOUR_API_KEY";
          const map = new Map({
            basemap: "arcgis-topographic" // Basemap layer service
          const view = new MapView({
            container: "viewDiv",
            map: map,
            center: [-118.80543,34.02700], // Longitude, latitude
            zoom: 12
          // Create a UI with the filter expressions
          const sqlExpressions = ["Choose a SQL where clause...", "Roll_LandValue < 200000", "TaxRateArea = 10853", "Bedrooms5 > 0", "UseType = 'Residential'", "Roll_RealEstateExemp > 0"];
          // UI
          const selectFilter = document.createElement("select");
          selectFilter.setAttribute("class", "esri-widget esri-select");
          selectFilter.setAttribute("style", "width: 275px; font-family: Avenir Next W00; font-size: 1em;");
          sqlExpressions.forEach(function(sql){
            let option = document.createElement("option");
          view.ui.add(selectFilter, "top-right");
    
          // Add a feature layer to map with all features visible on client (no filter)
          const featureLayer = new FeatureLayer({
            url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0",
            outFields: ["*"],
            popupTemplate: {
              title: "{UseType}",
              content: "Description: {UseDescription}. Land value: {Roll_LandValue}"
            },
            definitionExpression: "1=0"
          });
          map.add(featureLayer);
    
          // Server-side filter
          function setFeatureLayerFilter(expression) {
            featureLayer.definitionExpression = expression;
          }
    
          // Event listener
          selectFilter.addEventListener('change', function (event) {
      </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>
  2. Add an event listener to call the setFeatureLayerFilter function when a SQL where clause is chosen from the selector.

    Add line.Add line.Add line.Add line.
                                                                                             
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    <html>
    <head>
      <meta name="description" content="ArcGIS JavaScript Tutorials: Filter a feature layer">
      <meta charset="utf-8">
      <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
      <title>ArcGIS API for JavaScript Tutorials: Filter a feature layer with SQL (JSAPI)</title>
      <style>
        html,
        body,
        #viewDiv {
          padding: 0;
          margin: 0;
          height: 100%;
          width: 100%;
      </style>
      <link rel="stylesheet" href="https://js.arcgis.com/4.20/esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.20/"></script>
    </head>
    
    <script>
      require([
          "esri/config",
          "esri/Map",
          "esri/views/MapView",
          "esri/layers/FeatureLayer"
        ], function(esriConfig,Map,MapView,FeatureLayer) {
          esriConfig.apiKey = "YOUR_API_KEY";
          const map = new Map({
            basemap: "arcgis-topographic" // Basemap layer service
          const view = new MapView({
            container: "viewDiv",
            map: map,
            center: [-118.80543,34.02700], // Longitude, latitude
            zoom: 12
          // Create a UI with the filter expressions
          const sqlExpressions = ["Choose a SQL where clause...", "Roll_LandValue < 200000", "TaxRateArea = 10853", "Bedrooms5 > 0", "UseType = 'Residential'", "Roll_RealEstateExemp > 0"];
          // UI
          const selectFilter = document.createElement("select");
          selectFilter.setAttribute("class", "esri-widget esri-select");
          selectFilter.setAttribute("style", "width: 275px; font-family: Avenir Next W00; font-size: 1em;");
          sqlExpressions.forEach(function(sql){
            let option = document.createElement("option");
          view.ui.add(selectFilter, "top-right");
          // Add a feature layer to map with all features visible on client (no filter)
          const featureLayer = new FeatureLayer({
            url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0",
            outFields: ["*"],
            popupTemplate: {
              title: "{UseType}",
              content: "Description: {UseDescription}. Land value: {Roll_LandValue}"
            definitionExpression: "1=0"
          // Server-side filter
          function setFeatureLayerFilter(expression) {
            featureLayer.definitionExpression = expression;
          }
    
          // Event listener
          selectFilter.addEventListener('change', function (event) {
            setFeatureLayerFilter(event.target.value);
          });
      </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>

Run the app

In CodePen, run your code to display the map.

When the map displays, you should be able to choose a SQL query from the selector that applies a definition expression to the visible extent of the map. Only the features that match are added to the feature layer and displayed in the view.

What's next?

Learn how to use additional API features and ArcGIS services in these tutorials:

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.