0
0
Node.jsframework~10 mins

Parsing query strings in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Parsing query strings
Start with URL string
Extract query string part
Split by '&' to get key=value pairs
For each pair: split by '=' to separate key and value
Decode key and value
Store in object as key: value
Return parsed object
This flow shows how a URL's query string is taken apart step-by-step to get keys and values in an object.
Execution Sample
Node.js
const url = 'https://example.com?name=John&age=30';
const queryString = url.split('?')[1];
const pairs = queryString.split('&');
const params = {};
pairs.forEach(pair => {
  const [key, value] = pair.split('=');
  params[decodeURIComponent(key)] = decodeURIComponent(value);
});
This code takes a URL, extracts the query string, splits it into pairs, decodes them, and stores them in an object.
Execution Table
StepActionInputOutput/State
1Start with URL stringhttps://example.com?name=John&age=30URL string stored
2Extract query stringSplit by '?'name=John&age=30
3Split query string by '&'name=John&age=30['name=John', 'age=30']
4Process first pairname=Johnkey='name', value='John'
5Decode and store first pairkey='name', value='John'params = { name: 'John' }
6Process second pairage=30key='age', value='30'
7Decode and store second pairkey='age', value='30'params = { name: 'John', age: '30' }
8Return parsed objectparams{ name: 'John', age: '30' }
💡 All key=value pairs processed and stored in params object
Variable Tracker
VariableStartAfter Step 5After Step 7Final
url'https://example.com?name=John&age=30''https://example.com?name=John&age=30''https://example.com?name=John&age=30''https://example.com?name=John&age=30'
queryStringundefined'name=John&age=30''name=John&age=30''name=John&age=30'
pairsundefined['name=John', 'age=30']['name=John', 'age=30']['name=John', 'age=30']
params{}{ name: 'John' }{ name: 'John', age: '30' }{ name: 'John', age: '30' }
keyundefined'name''age'undefined
valueundefined'John''30'undefined
Key Moments - 3 Insights
Why do we split the query string by '&'?
Because each key=value pair in the query string is separated by '&', splitting by '&' isolates each pair for processing (see execution_table step 3).
Why do we use decodeURIComponent on keys and values?
Because query strings can have encoded characters (like spaces as %20), decoding them converts these back to normal characters for correct keys and values (see execution_table steps 5 and 7).
What happens if the URL has no query string?
Splitting by '?' would give undefined or no second part, so the code should check for this to avoid errors. In this example, we assume a query string exists.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what are the key and value extracted from the first pair?
Akey='John', value='name'
Bkey='age', value='30'
Ckey='name', value='John'
Dkey='name', value='age'
💡 Hint
Check the 'Process first pair' row in execution_table
At which step does the params object first get a key-value pair added?
AStep 5
BStep 3
CStep 6
DStep 7
💡 Hint
Look for when params changes from empty to having 'name' key in execution_table
If the URL had no query string, what would happen when splitting by '?'?
Aparams would have one empty key
BqueryString would be undefined or missing
Cpairs would be an empty array
DThe code would work normally
💡 Hint
Consider what split('?')[1] returns if '?' is not in the URL
Concept Snapshot
Parsing query strings:
- Extract part after '?' from URL
- Split by '&' to get key=value pairs
- Split each pair by '=' to separate key and value
- Decode keys and values
- Store in an object
- Result: easy access to query parameters
Full Transcript
Parsing query strings means taking the part of a URL after the question mark and turning it into an object with keys and values. We start with the full URL string, then split it at the question mark to get the query string. Next, we split that string by the ampersand character to get each key=value pair. For each pair, we split by the equal sign to separate the key and the value. We decode both to handle special characters. Then we store them in an object. This object lets us easily use the parameters in code. The process stops when all pairs are processed. If the URL has no query string, the code needs to handle that case to avoid errors.