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

292
Vistas
Ternary operator - when variable is undefined

I have a fn that creates and populate variable linkedinInsight when its invoked.

Just a quick overview of this fn, it will check the response data and see if there is a match. If yes, it would populate the linkedinInight variable with the data.

Using Ternary operation, I am unable to display "undefined" when the variable is undefined. linkedinInsight === undefined ? "undefined" : "Variable exist"

However, if the fn gets a match, I could see "Variable exist" displayed.

In the case where variable is undefined, from the console, it is showing

Error handling response: TypeError: Cannot read properties of undefined (reading 'insight_tags')
    at chrome-extension://fpncfpgjnkfnlafmojhhpgophpgikaao/popup.js:13:60

The objective here is to present the data and if we couldnt find data, I would like to return a "Data not found message".

Any help guys?

document.addEventListener(
  "DOMContentLoaded",
  function () {
    var checkPageButton = document.getElementById("clickIt");
    checkPageButton.addEventListener(
      "click",
      function () {
        chrome.tabs.getSelected(null, function (tab) {
          const backgroundPage = chrome.extension.getBackgroundPage();

          const linkedinInsight =
            backgroundPage["_linkedin_pixel_data"][tab.id].insight_tags;


          alert(
            linkedinInsight === undefined ? "undefined" : "Variable exist"
          );
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The issue is in below line, replace

const linkedinInsight =
            backgroundPage["_linkedin_pixel_data"][tab.id].insight_tags;

with

const linkedinInsight =
            (backgroundPage && backgroundPage["_linkedin_pixel_data"] && backgroundPage["_linkedin_pixel_data"][tab.id] && backgroundPage["_linkedin_pixel_data"][tab.id].insight_tags) || undefined;

Or if optional chaining is supported

const linkedinInsight =
                backgroundPage?.["_linkedin_pixel_data"]?.[tab.id]?.insight_tags;
about 4 years ago · Juan Pablo Isaza Denunciar

0

Use a nullish coalescingm operator

let exists = value ?? `${value} doesn't exist`;

If the value is undefined or null then return the value on the right side of operator ??

function check(X) {
  let present = X  ?? `"X" does not exist`;
  let limit = X < 10 && X > 0 ? `"X" is within range` :  `"X" is out of range`;
  if (typeof present == 'string') {
    console.log(present);
    return;
  }
  console.log(limit);
}

check(5);
check(10);
check(-1);
check();
check(null);

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