How to check if a variable exists with the Node.js Jade template engine?

Sometimes, we want to check if a variable exists with the Node.js Jade template engine.

In this article, we’ll look at how to check if a variable exists with the Node.js Jade template engine.

How to check if a variable exists with the Node.js Jade template engine?

To check if a variable exists with the Node.js Jade template engine, we can check if the property name with the variable name exists in the locals object.

For instance, we write

if locals.username
  p= username
else
  p No Username!

to check if the username template variable exists with if locals.username.

Conclusion

To check if a variable exists with the Node.js Jade template engine, we can check if the property name with the variable name exists in the locals object.