LOGBOOK

HELP

Quiz Entry - updated: 2026.06.20

How do you check if a query parameter exists and is not empty?

Test existence with the in operator and then compare the value against '' to confirm it is not empty.

// Check if parameter exists
if ('name' in req.query) {
    // Parameter was provided (might be empty string)
}

// Check if parameter exists AND has a value
if ('name' in req.query && req.query.name !== '') {
    // Parameter exists and is not empty
}

// Common pattern for required parameters
if (!('name' in req.query) || req.query.name === '') {
    res.status(400);
    res.send({ error: "Parameter 'name' is required" });
    return;
}

Why both checks?

  • ?name= - parameter exists but value is empty string
  • Parameter missing - req.query.name is undefined

Alternative using optional chaining:

if (!req.query.name?.trim()) {
    // Missing or empty (including whitespace-only)
}

From Quiz: WEBT / Backend Development | Updated: Jun 20, 2026