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

222
Vistas
How to know if an object property exists and has a value

It happens to me lots of times in javascript, php and other languages.

  1. I need to know if an object exists
  2. I need to know if that object has a property
  3. I need to check if the value of that property meets a condition

I use to do like this :

if (typeof myObject != "undefined") {
  if (myObject.hasOwnProperty('myProp') {
    if (myObject.myProp == "myTestValue") {
      // execute code
    }
  }
}

if I do just like this :

if (myObject.myProp == "myTestValue") {
  // execute code
}

it raises an error if object or property doesn't exist.

Is there a way of doing that with one line of code ?

Tks

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

0

Yes, you can use optional chaining operator for that

if (myObject?.myProp === "myTestValue") {
  // execute code
}
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