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

122
Vistas
if statement with multiple or conditions returning wrong value

I am pretty sure I am missing something basic here but I am having trouble with using multiple or or || operators with my if statement.

For some reason the if statement is not catching the name variable:

testword = "billy"

if ((testword != "billy") ||
    (testword != "tom") ||
    (testword != "sara") ||
    (testword != "michael")) {
console.log("none of the names match")
} else {
console.log("name found!")
}

When I try this I get none of the names match when I should get name found!

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Your logic is a bit convoluted

A far simpler approach to both write and understand is put all those names in an array and see if the array includes the testword. This is only a single boolean test

const testword = "billy",
  words = ["billy", "tom", "sara", "michael"]

if (words.includes(testword)) {
  console.log("name found!")
} else {
  console.log("none of the names match")
}

about 4 years ago · Santiago Trujillo Denunciar

0

The or operators execute to true when any of the condition is not equal to testword thus logging none of the names match

However, you can try to change your code to

testword = "billy"
names = ['billy','tom','sara','michael',]

if (names.indexOf(testword) != -1) {
console.log("name found!")
} else {
console.log("none of the names match")
}
about 4 years ago · Santiago Trujillo 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