Amarjit Singh
Amarjit Singh

Reputation: 2154

Express JS empty string instead of null in ejs templates

I have so many .ejs templates in an expressjs project and when some variable value is null. It just print "null" in place of variable. But I want nothing to be printed where a variable is null. For example: I have two variables first_name and last_name with values "John" and null. In this case I want only value of first_name to be printed But I am getting John null.

I know I can first check if a variable is not null then only I should display it. But as I said I have a project with so many ejs files. Therefore I want this behaviour as default. I mean some sort of configuration with which I can tell express to do it.

Upvotes: 1

Views: 1400

Answers (1)

Sankalpa Timilsina
Sankalpa Timilsina

Reputation: 341

Consider the following:

<% var firstName = 'John', lastName = null; %>
<%= firstName %><%= lastName %>

In this case the output will be John only i.e. not with the null printed. Make sure the lastName is not a string i.e. 'null'. In that case, the lastName will be printed.

Upvotes: 1

Related Questions