Avatar ·

How to get query string parameters in JavaScript?

Hello everyone! I'm a beginner in JavaScript and writing a simple single-page site. I have a URL like:\n\n```\nhttps://example.com/page?name=John&age=25&city=New+York\n```\n\nI need to extract the values of the `name`, `age`, and `city` parameters from the query string. I tried parsing `window.location.search` manually using `split('&')` and `split('=')`, but it works poorly: if the value contains characters like `+` or `%20`, everything breaks. Also, if a parameter is missing, the code throws an error.\n\nPlease advise how to correctly and reliably get query string parameters? Preferably with support for URL encoding and without third-party libraries (pure JavaScript). I would appreciate code examples and explanations.\n\n<<>>

Standard way: URLSearchParams

\n

Starting from modern browsers (and Node