Jade - Template Engine: How to check if a variable exists Jade - Template Engine: How to check if a variable exists express express

Jade - Template Engine: How to check if a variable exists


This should work:

- if (typeof(username) !== 'undefined'){  //-do something-}


Simpler than @Chetan's method if you don't mind testing for falsy values instead of undefined values:

if locals.username  p= usernameelse  p No Username!

This works because the somewhat ironically named locals is the root object for the template.


if 'username' in this    p=username

This works because res.locals is the root object in the template.