JavaScript - URL & Query Params

Overview

Estimated time: 10–15 minutes

JavaScript provides APIs for parsing and manipulating URLs and their query parameters.

Learning Objectives

  • Parse and build URLs using the URL API.
  • Read and set query parameters.

Prerequisites

URL API

const url = new URL('https://example.com?foo=1');
console.log(url.searchParams.get('foo')); // 1
url.searchParams.set('bar', '2');
console.log(url.toString());

Common Pitfalls

  • URL API is not supported in very old browsers.

Summary

The URL API makes it easy to work with URLs and query parameters in modern JavaScript.