Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

189
Vistas
Logical operator "!" not working in ejs file

This is the code

<% if( !title || title!=="login"){   %>   
<div class="dashboard-main-cont">
    <div class="navigation-option">
        <a class="btn btn-primary navbtn" href="/admin/bookings">Booking Requests</a>
        <a class="btn btn-primary navbtn" href="/admin/showprojects">Manage Projects</a>
        <a class="btn btn-primary navbtn" href="#">Calculation Requests</a>
        <a class="btn btn-primary navbtn" href="#">Manage Blogs</a>
        <form action="/admin/logout" method="POST">
            <button class="btn btn-danger">Logout</button>
        </form>
    </div>
<% } %> 

I am not passing a title value but I have put "!" in front of "title", but I am still getting this error:

title is not defined

How to solve this problem? Thanks in advance!!

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You probably should define title first using keywords like let or const. This error is coming because you have not defined it.

about 4 years ago · Juan Pablo Isaza Denunciar

0

So, I think that "title" needs to at least be declared for it to be able to work with the "!".

You could render the file by passing title: undefined on the render information. That way, the variable is defined as "undefined", but at least it is defined.

On the snippet below, by defining the variable, it can be used by the bang to detect if it is a "positive" value, as for the second "if", it throws an error.

const declared_title = undefined

console.log("foo")

if( !declared_title || declared_title !== "login") {
    console.log("bar")
}

console.log("fizz")

if( !undeclared_title || undeclared_title !== "login") {
    console.log("buzz")
}

As for the render call, it should look something like this

res.render('your_page',{ title: undefined });

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda