How to Create An Optional Parameter In Express?

How to Create An Optional Parameter In Express?

·

1 min read

You can set a parameter of a router as optional with the following syntax:

Syntax

localhost/route/:parameter?

To add an optional parameter for your route, add three things: a colon :, the parameter name and a question mark ? .

The colon : goes before the parameter name (this is common for all parameters) and the question mark ? goes after the parameter name, which makes it optional.

What Happens When You Don't Fill In An Optional Parameter?

If you don't fill in any value for that parameter, its value will be "undefined" but not error will be caused.