I want to use ternary operator in ejs. The following code block is not working. Should I only use if-else in ejs?
<% kindOfDay === "Saturday" || kindOfDay === "Sunday" ? { %>
<h1 style="color: purple;"><%= kindOfDay %> List</h1>
<% } : { %>
<h1 style="color: blue;"><%= kindOfDay %> List</h1>
<% } %>
The ternary operator creates a single expression and does not use { }.
It is used like condition? a : b or condition? (a) : (b) not condition? {a} : {b}.
If/else on the other hand, is used with curly braces like if (condition) {a} else {b}
However in your case, you can simplify your code to:
<h1 style="color: <%= (kindOfDay === "Saturday" || kindOfDay === "Sunday") ? "purple" : "blue" %>"><%= kindOfDay %> List</h1>