Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
How can I use ternary operator in ejs?

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>
<% } %>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!